﻿package  
{
	import adobe.utils.CustomActions;
	import flash.text.TextFormat;
	//import flash.desktop.Clipboard;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.net.URLLoader;
	import flash.net.sendToURL;
	import flash.net.navigateToURL;
	import flash.net.URLRequest ;
	import flash.net.URLVariables ;
	import flash.net.URLRequestMethod ;
	import flash.net.URLRequestHeader ;	
	import flash.sampler.DeleteObjectSample;
	import flash.system.Security;
	import flash.text.TextField;
	import flash.errors.IOError;
	import flash.errors.*;
	import gs.*;
	import flash.events.*;
	import gs.easing.*;
	import com.athena.vo.*;
	import com.athena.controller.*;
	import flash.display.*;
	import flash.utils.*;
	import flash.events.*;
	import fl.containers.ScrollPane; 
	import fl.controls.ScrollPolicy; 
	import fl.controls.DataGrid; 
	import fl.data.DataProvider;
	import flash.net.URLRequest;
	import flash.net.URLLoader;
	import com.adobe.serialization.json.JSON;
	import flash.text.TextFieldAutoSize;
	import flash.external.ExternalInterface;
	import flash.net.LocalConnection;
	
	public class ASPhomMain {
		//http://localhost/game/flash/
		//http://118.102.6.107/flash/Kick.mp3
		
		// Initialization:
		public static var ArrState : Array ;
		public static var MinCard : int ;
		public static var isU : Boolean = false ;
		public static var isView : Boolean = false ;
		public static var ArrStateStore : Array ;
		public static var CountSelection : int = 0 ;
		public static var TimerInTable:Timer = new Timer(1000);//Dieu khien thoi gian he thong		
		public static var TimerConection:Timer = new Timer(1000);//Dieu khien thoi gian he thong	
		public static var comdTimer:Timer = new Timer(1000) ; //Dieu khien thuc hien cac lenh
		public static var comdATTimer:Timer = new Timer(1000) ; //Dieu khien thuc hien cac lenh
		public static var chatTimer:Timer = new Timer(1000) ; //Dieu khien chat
		//public static var isSendTest : Boolean = false ;
		//public static var countConnect : int = 0 ;
		//public static var isReSend : Boolean = false;
		public static var isReConnect : Boolean = false ;
		public static var strGCOld : String = "" ;
		public static var strHCOld : String = "" ;
		public static var strSendOld : String  = "";
		public static var isSend : Boolean  = false;
		public static var isKickRoom : Boolean = false ;		
		public static var TakeTimer:Timer = new Timer(1000);//Dieu khien dong ho chay		
		public static var TakeCS : int = 0 ; //Vitri thang an
		public static var tChiaBai:Timer = new Timer(1000); //Dieu khien chia bai
		public static var mainTimer:Timer = new Timer(1000);//Dieu khien dong ho chay
		public static var currentTimerRun : int ; //Vitri dieu khien
		public static var currentAction : String = "Danh"; //Hanh dong hien tai - Boc || Danh
		public static var isTranfer : Boolean = false ;
		public static var cardMove : Card ;
		public static var csMove : int ;
		public static var isBegin : Boolean = true;
		public static var mainStage_mc : MovieClip;		
		public static var mainSocket:CustomSocket;
		public static var strTemp;
		public static var levelID = 0;
		public static var roomID = 0;
		public static var sortType :Number = 0 ;
		public static var tableID;
		
		public static var scrollPaneTable_mc:MovieClip;
		public static var scrTable:ScrollPane = new ScrollPane(); 
		public static var scrollPaneRoom_mc:MovieClip;
		public static var scrRoom:ScrollPane = new ScrollPane(); 	
		public static var scrollHistoryTF_mc:MovieClip;
		public static var scrHistoryTF:ScrollPane = new ScrollPane(); 
		public static var playerCards : Array = new Array() ;		
		public static var movieHistoryTF : Array = new Array() ;
		public static var movieRooms : Array = new Array() ;
		public static var movieTables : Array = new Array() ;
		public static var movieTablesXY : Array = new Array() ;
		public static var positionCards:Array = new Array();
		public static var arrCT_Trongngay : Array = new Array() ;
		public static var arrCT_Homtruoc : Array = new Array() ;
		public static var arrCT_Tuan : Array = new Array() ;
		public static var arrCT_Thang : Array = new Array() ;
		public static var arrDG_Trongngay : Array = new Array() ;
		public static var arrDG_Homtruoc : Array = new Array() ;
		public static var arrDG_Tuan : Array = new Array() ;
		public static var arrDG_Thang : Array = new Array() ;		
		public static var arrLevelInfor : Array = new Array() ;
		public static var objPosition:Object = new Object();		
		/************************/
		public static var currTable:ObjectTable = new ObjectTable();
		public static var currPlayer:ObjectPlayer = new ObjectPlayer();
		public static var currLevel:ObjectLevelRoom ;
		public static var currRoom:ObjectRoom ;
		public static var currPosition : int ; //Vi tri trong van bai
		public static var AGTransfer: Number = 0 ;
		
		public static var isSound : Boolean = true ;
		public static var msgSound : Sound ;
		public static var takeSound : Sound ;
		public static var removeSound : Sound ;
		public static var downSound : Sound ;
		public static var addSound : Sound ;
		public static var kickSound : Sound ;
		public static var finishedSound : Sound ;
		
		public static var markInit : Number = 0 ;
		public static var isDisplayEmotion : Boolean = false ; //Kiem soat viec an hien bang chon Emotion
		public static var nameEmotion : String = "" ;		
		public static var xEmotion : Number = 0 ;
		public static var yEmotion : Number = 0 ;
		public static var xLeftEmotionG : Number = 35 ;
		public static var yLeftEmotionG : Number = 250 ;
		public static var xRightEmotionG : Number = 725 ;
		public static var yRightEmotionG : Number = 255 ;
		public static var xTopEmotionG : Number = 200 ;
		public static var yTopEmotionG : Number = 20 ;	
		public static var xBottomEmotionG : Number = 570 ;
		public static var yBottomEmotionG : Number = 460 ;	
		public static var emotionTimerBottom:Timer = new Timer(1000);//Dieu khien dong ho chay
		public static var emotionTimerRight:Timer = new Timer(1000);//Dieu khien dong ho chay
		public static var emotionTimerLeft:Timer = new Timer(1000);//Dieu khien dong ho chay
		public static var emotionTimerTop:Timer = new Timer(1000);//Dieu khien dong ho chay
		public static var emotionPosition : int = 0 ; //Vi tri hien thi Emotion 0 - Bottom, 1- Right, 2- Top, 3 - Left
		
		public static var ArrEmotion : Array ; //Bang danh sach Emotion
		public static var ArrBG : Array ; //Danh sach cac image Load duoc
		
		public static var preURLSound : String = "http://tala.athena.vn/flash/" ;
		public static var preURLFeed : String = "http://tala.athena.vn/feed.aspx" ;
		public static var preURLEmotion : String = "http://tala.athena.vn/flash/Emotion/" ;
		public static var preURLImage : String = "./images/BackGround/" ;
		
		public static var cardX : Array = [280, 620, 310, 120, 340, 497, 340, 188, 500, 500, 195, 182] ;
		public static var cardY : Array = [470, 176, 28, 176, 360, 246, 140, 246, 370, 104, 104, 365] ;
		public static var ArrNameEmotions : Array = ["Emotion7.swf","Emotion8.swf","Emotion9.swf","Emotion10.swf","Emotion11.swf","Emotion12.swf","Emotion13.swf","Emotion14.swf","Emotion15.swf"] ;		
		public static var ArrNameBG : Array = ["BG.jpg", "Logo_Tala.png", "Khung_BB.png", "Khung_CT.png", "Khung_Tin.swf", "Khung_Top.png", "BGListTable.png", "BG_Banchoi_ND.jpg", "BG_Banchoi.jpg"] ;
		
		public static var ArrPopupName : Array = ["pp_Taoban.png", "pp_Vaoban.png", "pp_Khoebanbe.png", "pp_Ketqua.png"] ;
		public static var ArrPopupBG : Array ;
		public static var numberWarning : int = 0 ;
		
		public static var port:int;
		public static var host:String;//"athenavn.no-ip.org" ;//"localhost";
		public static var user:UserInfo;
		public static var vipMark : Number = 1000000 ;		
		public static var tf:TextField = new TextField();
		public static var myXML:XML = null;
		public static var TitleCaoThu : String = "" ;
		public static var imageLoaderMain:Loader;
		public static var arrQC : Array ; 
		public static var imageMain:URLRequest ;
		
		//public static var imageLoaderMain1:Loader;
		//public static var arrQC1 : Array ; 
		//public static var imageMain1:URLRequest ;
		
		public static var UName :String = "";
		public static var ChotName :String = "";
		public static var InitMoney : int = 0 ;
		public static var PlayMoney : int = 0 ;
		public static var NonPlayMoney: int = 0;
		
		public static var strMSG : String = "" ;
		public static var AllowExit : Boolean = true ;
		public static var ArrExitTable : Array ;
		public static var ArrUpdateMoney : Array ;
		
		
		public function ASPhomMain(main:MovieClip)
		{
			mainStage_mc = main;
			isReConnect = false ;
			mainStage_mc.bgMain_mc.visible = false;
			mainStage_mc.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove) ;
			mainStage_mc.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown) ;
			mainStage_mc.addEventListener(MouseEvent.MOUSE_UP, mouseUp) ;	
			arrLevelInfor = new Array() ;
			ArrEmotion = new Array() ;
			ArrBG = new Array() ;
			ArrPopupBG = new Array() ;
			arrQC = new Array() ;
				//arrQC1 = new Array() ;
			numberWarning = 0 ;
			//mainStage_mc.addEventListener(MouseEvent.CLICK, mouseKick) ;
			isSound = true ;
			//isSendTest = false ;
			tf.autoSize = TextFieldAutoSize.LEFT;
			tf.textColor = 0xFFFFFF;
			tf.border = true;			
			mainStage_mc.addChild(tf);	
			var paramObj:Object = LoaderInfo(mainStage_mc.loaderInfo).parameters;
			if (paramObj["session_id"] != undefined && paramObj["sign_user"]!= undefined && paramObj["username"] !=undefined )
			{
				var sessId:String = String(paramObj["session_id"]);
				var signId:String = String(paramObj["sign_user"]);
				var userName:String = String(paramObj["username"]);				
				var ip:String = String(paramObj["may"]);
				var port1:String = String(paramObj["cong"]);
				user = new UserInfo();
				user.SessionId = sessId;
				user.UserId = signId;
				user.Username = userName;
				if (ip != "")
					host = ip;
				if (port1 != "")
					port = new int(port1);
				if (TimerConection.running)
					TimerConection.stop() ;
				//countConnect = 0 ;
				TimerConection = new Timer(1000) ;
				TimerConection.repeatCount = 31 ;			
				TimerConection.addEventListener(TimerEvent.TIMER, StartTimerConection);				
				TimerConection.start() ;
			}
			LoadXML() ;
			LoadBackGround(0) ; //Bat dau Load BackGround
		}
		public static function LoadXML() : void {		
			//tf.appendText("==>abc") ;
			var myLoader:URLLoader = new URLLoader();
			myLoader.load(new URLRequest(preURLImage + "InputTL.xml"));
			myLoader.addEventListener(Event.COMPLETE, processXML);
			function processXML(e:Event):void {
				myXML = new XML(e.target.data);
				//tf.appendText(myXML) ;
			}
		}
		public static function LoadData() : void {
			if (myXML == null) return ;
			var strMain : String = "	" ;	
			for each (var hotNew:XML in myXML.children()){
				if (hotNew.name().toString() == "HOTNEWS"){
					for (var i:int = 0; i<hotNew.*.length(); i++){
						var str : String = "		" ;
						if (hotNew.CONTENT[i].Text[0] == "BR") {
							str = "<br/>" ;
							str = str + "		" ;
						} else {
							str = "<font color=\"" + hotNew.CONTENT[i].Color[0] + "\" face=\"" 
									+ hotNew.CONTENT[i].Font[0] + "\" Size=\"" + hotNew.CONTENT[i].Size[0] + "\">" ;
							if (hotNew.CONTENT[i].Bold[0] == 1) str = str + "<b>" ;
							str = str + hotNew.CONTENT[i].Text[0] ;
							if (hotNew.CONTENT[i].Bold[0] == 1) str = str + "</b>" ;
							str = str + "</font>" ;
						}
						strMain = strMain + " " + str ;
					}
					mainStage_mc.New_txt.Content_txt.htmlText = strMain ;
				} else {
					var formatText : TextFormat = new TextFormat() ;				
					formatText.font = "Times New Roman" ;
					formatText.bold = true ;
					formatText.italic = true ;
					formatText.size = 20 ;
					mainStage_mc.New_mc.Title_txt.text = hotNew.Title[0] ;
					mainStage_mc.New_mc.Title_txt.setTextFormat(formatText) ;
					TitleCaoThu = hotNew.Title[0] ;
					var formatText1 : TextFormat = new TextFormat() ;				
					formatText1.font = "Tahoma" ;
					formatText1.bold = true ;			
					formatText1.size = 13 ;
					for each(var person:XML in hotNew.children()) {
						if (person.name().toString() == "Persons") {
							for (var i:int = 0; i < person.*.length(); i++) {
								if (i > 10) break ;
								if (i < 10)
								{
									mainStage_mc.New_mc["Name" + i.toString() + "_txt"].text = person.Person[i].Name[0] ;
									mainStage_mc.New_mc["Mark" + i.toString() + "_txt"].text = person.Person[i].Mark[0] ;
									mainStage_mc.New_mc["Money" + i.toString() + "_txt"].text = person.Person[i].Money[0] ;
								}
								else if (i == 10)
								{
									mainStage_mc.New_mc["Name" + i.toString() + "_txt"].text = person.Person[i].Name[0] ;
									mainStage_mc.New_mc["Mark" + i.toString() + "_txt"].text = person.Person[i].Mark[0] ;
									mainStage_mc.New_mc["Money" + i.toString() + "_txt"].text = person.Person[i].Money[0] ;	
								}
								mainStage_mc.New_mc["Name" + i.toString() + "_txt"].setTextFormat(formatText1) ;
								mainStage_mc.New_mc["Mark" + i.toString() + "_txt"].setTextFormat(formatText1) ;
								mainStage_mc.New_mc["Money" + i.toString() + "_txt"].setTextFormat(formatText1) ;
							}		
						}
					}
				}
			}
		}
		public static function LoadBackGround(csB : int) : void {
			var imageLoader:Loader = new Loader();
			var image:URLRequest = new URLRequest(preURLImage + ArrNameBG[csB]) ;	
			imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoader);	
			imageLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
			imageLoader.name = "BG" ;
			imageLoader.load(image);
			function errorHandler(event:ErrorEvent) {
				LoadBackGround(csB) ;
			}
			function onLoader(evt:Event)
			{				
				imageLoader.x = 0 ;
				imageLoader.y = 0 ;
				ArrBG.push(imageLoader) ;
				if ((csB == 0) && (mainStage_mc.currentFrame == 2) || (mainStage_mc.currentFrame == 3) || (mainStage_mc.currentFrame == 4)) {
					mainStage_mc.BG_mc.addChild(ArrBG[0]) ;
				}
				if ((csB == 1) && (mainStage_mc.currentFrame == 3)) {
					mainStage_mc.BGLogo_mc.addChild(ArrBG[1]) ;
				}
				if ((csB == 2) && (mainStage_mc.currentFrame == 3)) {
					mainStage_mc.BGBanbe_mc.addChild(ArrBG[2]) ;
				}
				if ((csB == 3) && (mainStage_mc.currentFrame == 3)) {
					mainStage_mc.BGCT_mc.addChild(ArrBG[3]) ;
				}
				if ((csB == 4) && (mainStage_mc.currentFrame == 3)) {
					mainStage_mc.BGNew_mc.addChild(ArrBG[4]) ;
				}				
				if ((csB == 5) && (mainStage_mc.currentFrame == 3)) {
					mainStage_mc.BGTop_mc.addChild(ArrBG[5]) ;
				}
				if ((csB == 6) && (mainStage_mc.currentFrame == 4)) {
					mainStage_mc.BGListTable_mc.addChild(ArrBG[6]) ;
				}
				if ((csB == 7) && (mainStage_mc.currentFrame == 5) && (levelID == 0)) {
					mainStage_mc.BG_mc.addChild(ArrBG[7]) ;
				}
				if ((csB == 8) && (mainStage_mc.currentFrame == 5) && (levelID != 0)) {
					mainStage_mc.BG_mc.addChild(ArrBG[8]) ;
				}
				if (csB < 8) {					
					LoadBackGround(csB + 1) ;
				} else {
					LoadBackGround_Popup(0) ;
				}
			}	
		}
		
		public static function LoadBackGround_Popup(csL : int) : void {
			var imageLoader:Loader = new Loader();
			var image:URLRequest = new URLRequest(preURLImage + ArrPopupName[csL]) ;	
			imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoader);	
			imageLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
			imageLoader.name = "BGPopup" ;
			imageLoader.load(image);
			function errorHandler(event:ErrorEvent) {
				LoadBackGround_Popup(csL) ;
			}
			function onLoader(evt:Event)
			{				
				imageLoader.x = 0 ;
				imageLoader.y = 0 ;
				ArrPopupBG.push(imageLoader) ;
				if (csL < 3) {					
					LoadBackGround_Popup(csL + 1) ;
				} else {
					LoadEmotionLibrary(0) ;
				}
			}
		}
		
		public static function LoadEmotionLibrary(csE : int) : void {
			var imageLoader:Loader = new Loader();
			var image:URLRequest = new URLRequest(preURLEmotion + ArrNameEmotions[csE]) ;	
			imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoader);				
			imageLoader.name = "Emotion" ;
			imageLoader.load(image);		
			var mcAdd : MovieClip = MovieClip(new Emotion_Khung()) ;
			function onLoader(evt:Event)
			{				
				imageLoader.x = 0 ;
				imageLoader.y = 0 ;
				mcAdd.addChild(imageLoader) ;
				ArrEmotion.push(mcAdd) ;
				if (csE < 8) {					
					LoadEmotionLibrary(csE + 1) ;
				}
			}	
		}
		/******************************** Lam tron va hien thi so ***************************************/		
		public static function numberFormat(number:Number, maxDecimals:int = 2, forceDecimals:Boolean = false, siStyle:Boolean = true):String {
			var i:int = 0, inc:Number = Math.pow(10, maxDecimals), str:String = String(Math.round(inc * Number(number))/inc);
			var hasSep:Boolean = str.indexOf(",") == -1, sep:int = hasSep ? str.length : str.indexOf(",");
			var ret:String = (hasSep && !forceDecimals ? "" : (siStyle ? "." : ",")) + str.substr(sep+1);
			if (forceDecimals) for (var j:int = 0; j <= maxDecimals - (str.length - (hasSep ? sep-1 : sep)); j++) ret += "0";
			while (i + 3 < (str.substr(0, 1) == "-" ? sep-1 : sep)) ret = (siStyle ? "," : ".") + str.substr(sep - (i += 3), 3) + ret;
			return str.substr(0, sep - i) + ret;
		}
		public static function trim( s:String ):String	{ //Ham chuan hoa string
		  return s.replace( /^([\s|\t|\n]+)?(.*)([\s|\t|\n]+)?$/gm, "$2" );
		}
		/******************************** Control Sound *************************************************/
		public static function PlaySoundKick(event : Event) {
			kickSound.play() ;
		}
		public static function SoundKick() {
			if (isSound) {
				try 
				{
					if (kickSound == null) {
						kickSound = new Sound(new URLRequest(preURLSound + "Kick.mp3"));
						kickSound.addEventListener(Event.COMPLETE, PlaySoundKick) ;
					} else {
						kickSound.play()
					}
				} catch (e:Error) { }
			}
		}
		
		public static function sendFirstConnect():void	{
			mainSocket.sendData("HL:BothServer"+JSON.encode(user));
		}
		
		public static function mouseMove(evt :MouseEvent) {
			if (mainStage_mc.currentFrame != 5) return ;
			if (currTable.Status != 1) return ;
			if ((isTranfer) && (!isView)) {
				var movieMove : MovieClip = MovieClip(mainStage_mc.getChildByName("MovieMove")) ;	
				if (movieMove == null) return ;
				movieMove.x = mainStage_mc.mouseX - 30;
				movieMove.y = mainStage_mc.mouseY - 40;
			}			
		}
		
		public static function mouseDown(evt :MouseEvent) {
			if (mainStage_mc.currentFrame != 5) return ;
			if (currTable.Status != 1) return ;
			if ((isTranfer) || (isView)) return ;
			if (!((mainStage_mc.mouseX > 280) && (mainStage_mc.mouseX < 515)
			  && (mainStage_mc.mouseY > 450) && (mainStage_mc.mouseY < 545))) return ;			
			var xm : Number = mainStage_mc.mouseX ;
			var ym : Number = mainStage_mc.mouseY ;
			if (currTable == null) return ;
			if (currTable.ArrPlayers == null) return ;
			if (currTable.ArrPlayers.length == 0) return ;
			if (currTable.ArrPlayers[currPosition].ArrCard == null) return ;
			if (currTable.ArrPlayers[currPosition].ArrCard.length == 0) return ;
			isTranfer = true ;
			for (var i:int = 0; i < currTable.ArrPlayers[currPosition].ArrCard.length; i++) 
			{
				var _mc:MovieClip = currTable.ArrPlayers[currPosition].ArrMovie[i] ;
				if (((xm >= _mc.x) && (xm <= _mc.x + 20) && (i < currTable.ArrPlayers[currPosition].ArrCard.length - 1)) ||
					((xm >= _mc.x) && (xm <= _mc.x + 65) && (i == currTable.ArrPlayers[currPosition].ArrCard.length - 1))) {
						if ((ym >= _mc.y) && (ym <= _mc.y + 84)) {
							csMove = i ;
							cardMove = new Card() ;
							cardMove.NumberCard = currTable.ArrPlayers[currPosition].ArrCard[i].NumberCard ;
							cardMove.Suit = currTable.ArrPlayers[currPosition].ArrCard[i].Suit ;
							cardMove.State = currTable.ArrPlayers[currPosition].ArrCard[i].State ;
							var movieMove : MovieClip = MovieClip(new card()) ;
							movieMove.name = "MovieMove" ;
							movieMove.vien_mc.visible = false ;
							movieMove.x = currTable.ArrPlayers[currPosition].ArrCard[i].X ;
							movieMove.y = currTable.ArrPlayers[currPosition].ArrCard[i].Y ;
							movieMove.gotoAndStop(cardMove.NumberCard + (cardMove.Suit - 1) * 13 + 1) ;
							mainStage_mc.addChild(movieMove) ;
							currTable.ArrPlayers[currPosition].ArrMovie[i].visible = false ;
							break ;
						}
					}				
			}			
		}
		
		public static function mouseUp(evt :MouseEvent = null) {	
			if (mainStage_mc.currentFrame != 5) return ;
			if (!isTranfer) return ;
			isTranfer = false ;
			var movieMove : MovieClip = MovieClip(mainStage_mc.getChildByName("MovieMove")) ;
			if (movieMove == null) return ;			
			mainStage_mc.removeChild(movieMove) ;	
			var xm : Number = movieMove.x ;
			var ym : Number = movieMove.y ;
			var t : Boolean = false ;
			var csRun : int ;					
			currTable.ArrPlayers[currPosition].ArrMovie[csMove].visible = true ;
			//Chinh lai con bai truoc khi nhac di chuyen
			for (var l:int = csMove; l < currTable.ArrPlayers[currPosition].ArrCard.length-1; l++) 
			{
				currTable.ArrPlayers[currPosition].ArrCard[l].NumberCard = currTable.ArrPlayers[currPosition].ArrCard[l + 1].NumberCard ;
				currTable.ArrPlayers[currPosition].ArrCard[l].Suit = currTable.ArrPlayers[currPosition].ArrCard[l + 1].Suit ;
				currTable.ArrPlayers[currPosition].ArrCard[l].State = currTable.ArrPlayers[currPosition].ArrCard[l + 1].State ;				
			}
			
			//Chinh lai con bai sau khi nhac di chuyen
			for (var i:int = 0; i < currTable.ArrPlayers[currPosition].ArrCard.length-1; i++) 
			{
				var _mc:MovieClip = currTable.ArrPlayers[currPosition].ArrMovie[i] ;
				if (((xm >= currTable.ArrPlayers[currPosition].ArrCard[i].X) && (xm <= currTable.ArrPlayers[currPosition].ArrCard[i].X + 20)) || (xm < 280)) {
					if (xm < 280) csRun = 0 ;
					else csRun = i + 1 ;
					if (csRun > csMove) csRun-- ;
					if ((csMove == 0) && (csRun > 0)) csRun-- ;
					for (var j:int = currTable.ArrPlayers[currPosition].ArrCard.length-1; j>csRun; j--) 
					{
						currTable.ArrPlayers[currPosition].ArrCard[j].NumberCard = currTable.ArrPlayers[currPosition].ArrCard[j - 1].NumberCard ;
						currTable.ArrPlayers[currPosition].ArrCard[j].Suit = currTable.ArrPlayers[currPosition].ArrCard[j - 1].Suit ;
						currTable.ArrPlayers[currPosition].ArrCard[j].State = currTable.ArrPlayers[currPosition].ArrCard[j - 1].State ;						
					}
					movieMove = _mc ;
					currTable.ArrPlayers[currPosition].ArrCard[csRun].NumberCard = cardMove.NumberCard ;
					currTable.ArrPlayers[currPosition].ArrCard[csRun].Suit = cardMove.Suit ;
					currTable.ArrPlayers[currPosition].ArrCard[csRun].State = cardMove.State ;
					t = true ;
					break ;
				}				
			}
			
			if (!t) {
				currTable.ArrPlayers[currPosition].ArrCard[currTable.ArrPlayers[currPosition].ArrCard.length-1].NumberCard = cardMove.NumberCard ;
				currTable.ArrPlayers[currPosition].ArrCard[currTable.ArrPlayers[currPosition].ArrCard.length-1].Suit = cardMove.Suit ;
				currTable.ArrPlayers[currPosition].ArrCard[currTable.ArrPlayers[currPosition].ArrCard.length-1].State = cardMove.State ;
			}
			//Gan lai toa do cho danh sach con bai ==> Thay doi Movie tuong ung
			for (var k:int = 0; k < currTable.ArrPlayers[currPosition].ArrCard.length; k++) 
			{
				//trace("======>" + k) ;
				currTable.ArrPlayers[currPosition].ArrCard[k].X = 280 + k * 20 ;
				currTable.ArrPlayers[currPosition].ArrCard[k].Y = 470 ;		
				currTable.ArrPlayers[currPosition].ArrMovie[k].X = 280 + k * 20 ;
				currTable.ArrPlayers[currPosition].ArrMovie[k].Y = 470;
				var position :Number = currTable.ArrPlayers[currPosition].ArrCard[k].NumberCard + (currTable.ArrPlayers[currPosition].ArrCard[k].Suit - 1) * 13 + 1 ;
				currTable.ArrPlayers[currPosition].ArrMovie[k].gotoAndStop(position) ;
				if (currTable.ArrPlayers[currPosition].ArrCard[k].State == 2)
					currTable.ArrPlayers[currPosition].ArrMovie[k].vien_mc.visible = true ;
				else
					currTable.ArrPlayers[currPosition].ArrMovie[k].vien_mc.visible = false ;
			}
		//	tf.appendText("mouse down");
			//trace("CS:" + csMove + "---" + csRun) ;
			if (csMove == csRun) {
				if (currTable.ArrPlayers[currPosition].ArrMovie[csMove].y == 470) 
					currTable.ArrPlayers[currPosition].ArrMovie[csMove].y = 450 ;
				else 
					currTable.ArrPlayers[currPosition].ArrMovie[csMove].y = 470 ;						
			}
			cardMove = null ;
		}
		/******************************************************************************************************/
		/******************************** Functions in Frame 3 ************************************************/
		/******************************************************************************************************/		
		public static function CheckExistTop25(name : String, arr : Array) : Boolean {
			for (var i:int = 0; i < arr.length; i++) 
			{
				if (arr[i].N == name) return true ;				
			}
			return false ;
		}
		public static function DisplayTopCT_Trongngay(arr : Array, type : int) : void {
			//tf.appendText("Cao thu trong ngay");
			if (mainStage_mc.currentFrame == 3) {
				for (var j:int = 0; j < 10; j++) 
				{
					mainStage_mc.CT_mc.CTTrongngay_mc["name" + j.toString() + "_txt"].text = "" ;
					mainStage_mc.CT_mc.CTTrongngay_mc["Result" + j.toString() + "_txt"].text = "";
				}
			}
			if (arr == null) return ;
			//tf.appendText("Length:" + arr.length) ;
			var formatText : TextFormat = new TextFormat() ;				
			formatText.font = "Tahoma" ;
			formatText.bold = true ;
			formatText.size = 12 ;
			var formatTextTop : TextFormat = new TextFormat() ;				
			formatTextTop.font = "Tahoma" ;
			formatTextTop.bold = true ;
			formatTextTop.size = 14 ;
			//{Hien thi giao dien
			if (mainStage_mc.currentFrame == 3) {
				for (var j:int = 0; j < arr.length; j++) 
				{
					if (j > 9) break ;
					var item = arr[j];
					mainStage_mc.CT_mc.CTTrongngay_mc["name" + j.toString() + "_txt"].text = item.N ;
					mainStage_mc.CT_mc.CTTrongngay_mc["Result" + j.toString() + "_txt"].text = numberFormat(item.A, 0, false, true) 
					if (type == 0)
						mainStage_mc.CT_mc.CTTrongngay_mc["Result" + j.toString() + "_txt"].text = mainStage_mc.CT_mc.CTTrongngay_mc["Result" + j.toString() + "_txt"].text + " AG" ;
					if ( j == 0 )
					{
						mainStage_mc.CT_mc.CTTrongngay_mc["name" + j.toString() + "_txt"].setTextFormat(formatTextTop) ;
						mainStage_mc.CT_mc.CTTrongngay_mc["Result" + j.toString() + "_txt"].setTextFormat(formatTextTop) ;
					} else{
						mainStage_mc.CT_mc.CTTrongngay_mc["name" + j.toString() + "_txt"].setTextFormat(formatText) ;
						mainStage_mc.CT_mc.CTTrongngay_mc["Result" + j.toString() + "_txt"].setTextFormat(formatText) ;
					}
					
				}
			}
			//}
		}
		public static function DisplayTopCT_Homtruoc(arr : Array) : void {
			if (mainStage_mc.currentFrame == 3) {
				for (var j:int = 0; j < 10; j++) 
				{
					mainStage_mc.CT_mc.CTHomtruoc_mc["name" + j.toString() + "_txt"].text = "" ;
					mainStage_mc.CT_mc.CTHomtruoc_mc["Result" + j.toString() + "_txt"].text = ""; 
					mainStage_mc.CT_mc.CTHomtruoc_mc["Mark" + j.toString() + "_txt"].text = "" ;
				}
			}
			if (arr == null) return ;
			var formatText : TextFormat = new TextFormat() ;				
			formatText.font = "Tahoma" ;
			formatText.bold = true ;
			formatText.size = 12 ;
			var formatTextTop : TextFormat = new TextFormat() ;				
			formatTextTop.font = "Tahoma" ;
			formatTextTop.bold = true ;
			formatTextTop.size = 14 ;
			//{Hien thi giao dien
			//tf.appendText("\n top hom truoc");
			if (mainStage_mc.currentFrame == 3) {
				for (var j:int = 0; j < arr.length; j++) 
				{
					if (j > 9) break ;
					var item = arr[j];
					mainStage_mc.CT_mc.CTHomtruoc_mc["name" + j.toString() + "_txt"].text = item.N ;
					mainStage_mc.CT_mc.CTHomtruoc_mc["Result" + j.toString() + "_txt"].text = numberFormat(item.A, 0, false, true)  + " AG"; 
					mainStage_mc.CT_mc.CTHomtruoc_mc["Mark" + j.toString() + "_txt"].text = item.M ;
					if ( j == 0)
					{
						mainStage_mc.CT_mc.CTHomtruoc_mc["name" + j.toString() + "_txt"].setTextFormat(formatTextTop) ;
						mainStage_mc.CT_mc.CTHomtruoc_mc["Result" + j.toString() + "_txt"].setTextFormat(formatTextTop) ;
						mainStage_mc.CT_mc.CTHomtruoc_mc["Mark" + j.toString() + "_txt"].setTextFormat(formatTextTop) ;
					}
					else {
						mainStage_mc.CT_mc.CTHomtruoc_mc["name" + j.toString() + "_txt"].setTextFormat(formatText) ;
						mainStage_mc.CT_mc.CTHomtruoc_mc["Result" + j.toString() + "_txt"].setTextFormat(formatText) ;
						mainStage_mc.CT_mc.CTHomtruoc_mc["Mark" + j.toString() + "_txt"].setTextFormat(formatText) ;
						}
				}
			}
			//}
		}
		
		public static function DisplayTopCT_TuanThang(arr : Array) : void { 
			//tf.appendText("\n Nhay vao ct tuan")
			if (mainStage_mc.currentFrame == 3) {
				for (var j:int = 0; j < 10; j++) 
				{
					mainStage_mc.CT_mc.CTTuanThang_mc["name" + j.toString() + "_txt"].text = "" ;
					mainStage_mc.CT_mc.CTTuanThang_mc["Result" + j.toString() + "_txt"].text = ""; 
					mainStage_mc.CT_mc.CTTuanThang_mc["Mark" + j.toString() + "_txt"].text = "" ;
				}
			}
			if (arr == null) return ;
			var formatText : TextFormat = new TextFormat() ;				
			formatText.font = "Tahoma" ;
			formatText.bold = true ;
			formatText.size = 12 ;
			var formatTextTop : TextFormat = new TextFormat() ;				
			formatTextTop.font = "Tahoma" ;
			formatTextTop.bold = true ;
			formatTextTop.size = 14 ;
			//{Hien thi giao dien
			//tf.appendText("\n arr.length = " + arr.length.toString());
			//mainStage_mc.CT_mc.CTTuanThang_mc.Title_txt.text = TitleCaoThu; 
			if (mainStage_mc.currentFrame == 3) {
				for (var j:int = 0; j < arr.length; j++) 
				{
					if (j > 10) break ;
					var item = arr[j];
					mainStage_mc.CT_mc.CTTuanThang_mc["name" + j.toString() + "_txt"].text = item.N ;
					//mainStage_mc.CT_mc.CTTuanThang_mc["Result" + j.toString() + "_txt"].text = numberFormat(item.A, 0, false, true)  + " AG"; 
					mainStage_mc.CT_mc.CTTuanThang_mc["Mark" + j.toString() + "_txt"].text = item.A ;
					
					if ( j == 0)
					{
						mainStage_mc.CT_mc.CTTuanThang_mc["name" + j.toString() + "_txt"].setTextFormat(formatTextTop) ;
						mainStage_mc.CT_mc.CTTuanThang_mc["Result" + j.toString() + "_txt"].setTextFormat(formatTextTop) ;
						mainStage_mc.CT_mc.CTTuanThang_mc["Mark" + j.toString() + "_txt"].setTextFormat(formatTextTop) ;
					}
					if ( j == 10)
					{
						mainStage_mc.CT_mc.CTTuanThang_mc["name" + j.toString() + "_txt"].setTextFormat(formatText) ;
						mainStage_mc.CT_mc.CTTuanThang_mc["Result" + j.toString() + "_txt"].setTextFormat(formatText) ;
						mainStage_mc.CT_mc.CTTuanThang_mc["Mark" + j.toString() + "_txt"].setTextFormat(formatText) ;
					}
					else {
						mainStage_mc.CT_mc.CTTuanThang_mc["name" + j.toString() + "_txt"].setTextFormat(formatText) ;
						mainStage_mc.CT_mc.CTTuanThang_mc["Result" + j.toString() + "_txt"].setTextFormat(formatText) ;
						mainStage_mc.CT_mc.CTTuanThang_mc["Mark" + j.toString() + "_txt"].setTextFormat(formatText) ;
					}
				}
			}
			//}
		}
		
			public static function DisplayTopCT_Thang(arr : Array) : void {
			//tf.appendText("hiện danh sách");
			if (mainStage_mc.currentFrame == 3) {
				for (var j:int = 0; j < 10; j++) 
				{
					mainStage_mc.CT_mc.CTThang_mc["name" + j.toString() + "_txt"].text = "" ;
					mainStage_mc.CT_mc.CTThang_mc["Result" + j.toString() + "_txt"].text = ""; 
					mainStage_mc.CT_mc.CTThang_mc["Mark" + j.toString() + "_txt"].text = "" ;
				}
			}
			//tf.appendText("hiện danh sách 2");
			//tf.appendText("arr " + arr.length());
			if (arr == null) return ;
			var formatText : TextFormat = new TextFormat() ;				
			formatText.font = "Tahoma" ;
			formatText.bold = true ;
			formatText.size = 12 ;
			var formatTextTop : TextFormat = new TextFormat() ;				
			formatTextTop.font = "Tahoma" ;
			formatTextTop.bold = true ;
			formatTextTop.size = 14 ;
			//{Hien thi giao dien
				//tf.appendText("hiện danh sách 3");
			var LastMonth: Date = new Date();
			var month = (LastMonth.getMonth()).toString();
			mainStage_mc.CT_mc.CTThang_mc.Title_txt.text = "Top Cao thủ Tá lả tháng " + month;
			if (mainStage_mc.currentFrame == 3) {
				for (var j:int = 0; j < arr.length; j++) 
				{
					if (j > 9) break ;
					var item = arr[j];
					mainStage_mc.CT_mc.CTThang_mc["name" + j.toString() + "_txt"].text = item.N ;
					//mainStage_mc.CT_mc.CTTuanThang_mc["Result" + j.toString() + "_txt"].text = numberFormat(item.A, 0, false, true)  + " AG"; 
					mainStage_mc.CT_mc.CTThang_mc["Mark" + j.toString() + "_txt"].text = item.A ;
					
					if ( j == 0)
					{
						mainStage_mc.CT_mc.CTThang_mc["name" + j.toString() + "_txt"].setTextFormat(formatTextTop) ;
						mainStage_mc.CT_mc.CTThang_mc["Result" + j.toString() + "_txt"].setTextFormat(formatTextTop) ;
						mainStage_mc.CT_mc.CTThang_mc["Mark" + j.toString() + "_txt"].setTextFormat(formatTextTop) ;
					}
					else {
						mainStage_mc.CT_mc.CTThang_mc["name" + j.toString() + "_txt"].setTextFormat(formatText) ;
						mainStage_mc.CT_mc.CTThang_mc["Result" + j.toString() + "_txt"].setTextFormat(formatText) ;
						mainStage_mc.CT_mc.CTThang_mc["Mark" + j.toString() + "_txt"].setTextFormat(formatText) ;
					}
				}
			}
			//}
		}
		public static function DisplayTopDG_Trongngay(arr : Array, type : int) : void {
			//tf.appendText("Top dai gia trong ngay");
			if (mainStage_mc.currentFrame == 3) {
				for (var j:int = 0; j < 10; j++) 
				{
					mainStage_mc.CTDG_mc.DGTrongngay_mc["name" + j.toString() + "_txt"].text = "" ;
					mainStage_mc.CTDG_mc.DGTrongngay_mc["Result" + j.toString() + "_txt"].text = ""; 
				}
			}
			if (arr == null) return ;			
			var formatText : TextFormat = new TextFormat() ;				
			formatText.font = "Tahoma" ;
			formatText.bold = true ;
			formatText.size = 12 ;
			var formatTextTop : TextFormat = new TextFormat() ;				
			formatTextTop.font = "Tahoma" ;
			formatTextTop.bold = true ;
			formatTextTop.size = 14 ;
			//{Hien thi giao dien
			if (mainStage_mc.currentFrame == 3) {
				for (var j:int = 0; j < arr.length; j++) 
				{
					if (j > 9) break ;
					var item = arr[j];
					mainStage_mc.CTDG_mc.DGTrongngay_mc["name" + j.toString() + "_txt"].text = item.N ;
					mainStage_mc.CTDG_mc.DGTrongngay_mc["Result" + j.toString() + "_txt"].text = numberFormat(item.A, 0, false, true); 
					if (type == 0)
						mainStage_mc.CTDG_mc.DGTrongngay_mc["Result" + j.toString() + "_txt"].text = mainStage_mc.CTDG_mc.DGTrongngay_mc["Result" + j.toString() + "_txt"].text + " AG" ;				
					if ( j == 0)
					{
						mainStage_mc.CTDG_mc.DGTrongngay_mc["name" + j.toString() + "_txt"].setTextFormat(formatTextTop) ;
						mainStage_mc.CTDG_mc.DGTrongngay_mc["Result" + j.toString() + "_txt"].setTextFormat(formatTextTop) ; }
					else {
						mainStage_mc.CTDG_mc.DGTrongngay_mc["name" + j.toString() + "_txt"].setTextFormat(formatText) ;
						mainStage_mc.CTDG_mc.DGTrongngay_mc["Result" + j.toString() + "_txt"].setTextFormat(formatText) ; 
						}
				}
			}
			//}
		}
		public static function DisplayTopDG_Homtruoc(arr : Array) : void {
			if (mainStage_mc.currentFrame == 3) {
				for (var j:int = 0; j < 10; j++) 
				{
					mainStage_mc.CTDG_mc.DGHomtruoc_mc["name" + j.toString() + "_txt"].text = "" ;
					mainStage_mc.CTDG_mc.DGHomtruoc_mc["Result" + j.toString() + "_txt"].text = ""; 
					mainStage_mc.CTDG_mc.DGHomtruoc_mc["Mark" + j.toString() + "_txt"].text = "" ;
				}
			}
			if (arr == null) return ;
			var formatText : TextFormat = new TextFormat() ;				
			formatText.font = "Tahoma" ;
			formatText.bold = true ;
			formatText.size = 12 ;
			//{Hien thi giao dien
			if (mainStage_mc.currentFrame == 3) {
				for (var j:int = 0; j < arr.length; j++) 
				{
					if (j > 9) break ;
					var item = arr[j];
					mainStage_mc.CTDG_mc.DGHomtruoc_mc["name" + j.toString() + "_txt"].text = item.N ;
					mainStage_mc.CTDG_mc.DGHomtruoc_mc["Result" + j.toString() + "_txt"].text = numberFormat(item.A, 0, false, true)  + " AG"; 
					mainStage_mc.CTDG_mc.DGHomtruoc_mc["Mark" + j.toString() + "_txt"].text = item.M ;
					mainStage_mc.CTDG_mc.DGHomtruoc_mc["name" + j.toString() + "_txt"].setTextFormat(formatText) ;
					mainStage_mc.CTDG_mc.DGHomtruoc_mc["Result" + j.toString() + "_txt"].setTextFormat(formatText) ;
					mainStage_mc.CTDG_mc.DGHomtruoc_mc["Mark" + j.toString() + "_txt"].setTextFormat(formatText) ;
				}
			}
			//}
		}
		
		public static function TopCT_Trongngay(str : String) : void {
			arrCT_Trongngay = new Array() ;
			//tf.appendText("==>STR:" + str) ;
			var arr : Array = (JSON.decode(str) as Array) ;
			for (var i:int = 0; i < arr.length; i++) 
			{
				//Check xem chua ton tai trong Danh sach chua ==> Add vao danh sach
				if (!CheckExistTop25(arr[i].N, arrCT_Trongngay)) {
					var obj : Object = new Object() ;
					obj.N = arr[i].N ;
					obj.A = arr[i].A ;
					arrCT_Trongngay.push(obj) ;
				}
				if (i == arr.length - 1)
					DisplayTopCT_Trongngay(arrCT_Trongngay, 0) ;
			}
		}
		public static function TopCT_Homtruoc(str : String) : void {
			arrCT_Homtruoc = new Array() ;
			var arr : Array = (JSON.decode(str) as Array) ;
			for (var i:int = 0; i < arr.length; i++) 
			{
				//Check xem chua ton tai trong Danh sach chua ==> Add vao danh sach
				if (!CheckExistTop25(arr[i].N, arrCT_Homtruoc)) {
					var obj : Object = new Object() ;
					obj.N = arr[i].N ;
					obj.A = arr[i].A ;
					obj.M = arr[i].M ;
					arrCT_Homtruoc.push(obj) ;
				}
			}
			DisplayTopCT_Homtruoc(arrCT_Homtruoc) ;
		}
		public static function TopCT_Tuan(str : String) : void {
			arrCT_Tuan = new Array() ;
			var arr : Array = (JSON.decode(str) as Array) ;
			//tf.appendText(str);
			for (var i:int = 0; i < arr.length; i++) 
			{
				//Check xem chua ton tai trong Danh sach chua ==> Add vao danh sach
				if (!CheckExistTop25(arr[i].N, arrCT_Tuan)) {
					var obj : Object = new Object() ;
					obj.N = arr[i].N ;
					obj.A = arr[i].A ;
					arrCT_Tuan.push(obj) ;
				}
			}
			DisplayTopCT_TuanThang(arrCT_Tuan) ;
		}
		public static function TopCT_Thang(str : String) : void {
			arrCT_Thang = new Array() ;
			var arr : Array = (JSON.decode(str) as Array) ;
			//tf.appendText("\n arrlength = " + arr.length.toString());
			//tf.appendText("\n lấy danh sách");
			for (var i:int = 0; i < arr.length; i++) 
			{
				//Check xem chua ton tai trong Danh sach chua ==> Add vao danh sach
				if (!CheckExistTop25(arr[i].N, arrCT_Thang)) {
					var obj : Object = new Object() ;
					obj.N = arr[i].N ;
					obj.A = arr[i].A ;
					arrCT_Thang.push(obj) ;
				}
				var dtCheck : Date = new Date() ;
				if (dtCheck.date == 1) {
						if (i == arr.length -1)
							DisplayTopCT_Thang(arrCT_Thang) ;
				}
			}	
			//tf.appendText("\n lấy danh sách 2");
			//DisplayTopCT_Trongngay(arrCT_Thang) ;
		}
		
		public static function TopDG_Trongngay(str : String) : void {
			arrDG_Trongngay = new Array() ;
			var arr : Array = (JSON.decode(str) as Array) ;
			for (var i:int = 0; i < arr.length; i++) 
			{
				//Check xem chua ton tai trong Danh sach chua ==> Add vao danh sach
				if (!CheckExistTop25(arr[i].N, arrDG_Trongngay)) {
					var obj : Object = new Object() ;
					obj.N = arr[i].N ;
					obj.A = arr[i].A ;
					arrDG_Trongngay.push(obj) ;
				}
				if (i == arr.length -1)
					DisplayTopDG_Trongngay(arrDG_Trongngay, 0) ;
			}
		}
		public static function TopDG_Homtruoc(str : String) : void {
			arrDG_Homtruoc = new Array() ;
			var arr : Array = (JSON.decode(str) as Array) ;
			for (var i:int = 0; i < arr.length; i++) 
			{
				//Check xem chua ton tai trong Danh sach chua ==> Add vao danh sach
				if (!CheckExistTop25(arr[i].N, arrDG_Homtruoc)) {
					var obj : Object = new Object() ;
					obj.N = arr[i].N ;
					obj.A = arr[i].A ;
					obj.M = arr[i].M ;
					arrDG_Homtruoc.push(obj) ;
				}
			}
			//DisplayTopDG_Homtruoc(arrDG_Homtruoc) ;
		}
		public static function TopDG_Tuan(str : String) : void {
			arrDG_Tuan = new Array() ;
			var arr : Array = (JSON.decode(str) as Array) ;
			for (var i:int = 0; i < arr.length; i++) 
			{
				//Check xem chua ton tai trong Danh sach chua ==> Add vao danh sach
				if (!CheckExistTop25(arr[i].N, arrDG_Tuan)) {
					var obj : Object = new Object() ;
					obj.N = arr[i].N ;
					obj.A = arr[i].A ;
					arrDG_Tuan.push(obj) ;
				}
			}
			//DisplayTopDG_Trongngay(arrDG_Tuan) ;
		}
		public static function TopDG_Thang(str : String) : void {
			arrDG_Thang = new Array() ;
			var arr : Array = (JSON.decode(str) as Array) ;
			for (var i:int = 0; i < arr.length; i++) 
			{
				//Check xem chua ton tai trong Danh sach chua ==> Add vao danh sach
				if (!CheckExistTop25(arr[i].N, arrDG_Thang)) {
					var obj : Object = new Object() ;
					obj.N = arr[i].N ;
					obj.A = arr[i].A ;
					arrDG_Thang.push(obj) ;
				}
			}
			//DisplayTopDG_Trongngay(arrDG_Thang) ;
		}
		
		public static function btnTransferAG_mc_Click(evt : MouseEvent) { //Chuyen AG
			//{ Tao Tieng
			SoundKick()
			//}
			
			if (mainStage_mc.currentFrame != 3) return ;
			mainStage_mc.KhungChuyenAG.visible = true ;
			mainStage_mc.New_mc.visible = false ;
			mainStage_mc.CTDG_mc.visible = false ;
			mainStage_mc.CT_mc.visible = false ;
			mainStage_mc.BGTop_mc.visible = false;
			mainStage_mc.btnCTDG.gotoAndStop(1) ;
			mainStage_mc.btnCT.gotoAndStop(1) ;
			mainStage_mc.btnNew.gotoAndStop(1) ;
			mainStage_mc.btnTransferAG.gotoAndStop(2) ;			
			mainStage_mc.KhungChuyenAG.addEventListener(KeyboardEvent.KEY_DOWN, keyPopupAG) ;			
			mainStage_mc.KhungChuyenAG.btnOK_mc.addEventListener(MouseEvent.CLICK, btnOK_AG_click);			
			mainStage_mc.KhungChuyenAG.btnNotOK_mc.addEventListener(MouseEvent.CLICK, btnNotOK_AG_click);
		}
		public static function keyPopupAG(evt : KeyboardEvent) { //Xu ly Phim tren popup chuyen AG
			if (evt.keyCode == 13) {
				var strName : String = mainStage_mc.KhungChuyenAG.name_txt.text ;			
				var rbg : Object = mainStage_mc.KhungChuyenAG.rb1.group ;				
				if (rbg.selection == null) return ;
				if (strName.length > 100)
					strName = strName.substring(0, 100) ;
				strName = trim(strName) ;
				if (strName.length < 1) return ;
				var object : Object = new Object() ;
				object.ID = user.PlayId ;
				object.AG = rbg.selection.value ;
				object.Name = strName ;
				AGTransfer = Number(object.AG) ;
				if ((currPlayer.Mark <= object.AG) || (strName == "")) {
					if (strName == "")
						Add_Popup_Thongbao("Tài khoản nhận phải khác rỗng!") ;
					else 
						Add_Popup_Thongbao("Bạn không đủ AG để chuyển!") ;
					return ;
				}
				strTemp = "TM:" + JSON.encode(object);
				mainSocket.sendData(strTemp);
				//Hien thi Man hinh Loading
				var tmpLD:MovieClip = MovieClip(new Loading());
				tmpLD.name = "Loading";
				tmpLD.x = 340;
				tmpLD.y = 250;			
				mainStage_mc.addChild(tmpLD);
			}			
		}		
		public static function btnOK_AG_click(evt:MouseEvent) { //Xu ly Kick Xac nhan tren popup chuyen tien
			//{ Tao Tieng
			SoundKick()
			//}
			var strName : String = mainStage_mc.KhungChuyenAG.name_txt.text ;			
			var rbg : Object = mainStage_mc.KhungChuyenAG.rb1.group ;
			if (rbg.selection == null) return ;
			if (strName.length > 100)
				strName = strName.substring(0, 100) ;
			strName = trim(strName) ;
			if (strName.length < 1) return ;
			var object : Object = new Object() ;
			object.ID = user.PlayId ;
			object.AG = rbg.selection.value ;
			object.Name = strName ;
			AGTransfer = object.AG ;
			if ((currPlayer.Mark <= object.AG) || (strName == "")) {
				if (strName == "")
					Add_Popup_Thongbao("Tài khoản nhận phải khác rỗng!") ;
				else 
					Add_Popup_Thongbao("Bạn không đủ AG để chuyển!") ;
				return ;
			}
			strTemp = "TM:" + JSON.encode(object);
			mainSocket.sendData(strTemp);
			//Hien thi Man hinh Loading
			var tmpLD:MovieClip = MovieClip(new Loading());
			tmpLD.name = "Loading";
			tmpLD.x = 340;
			tmpLD.y = 250;			
			mainStage_mc.addChild(tmpLD);	
			mainStage_mc.KhungChuyenAG.name_txt.text = "" ;			
		}
		public static function btnNotOK_AG_click(evt:MouseEvent) { //Xu ly Kick Huy bo tren popup chuyen tien
			mainStage_mc.KhungChuyenAG.name_txt.text = "" ;
			//{ Tao Tieng
			SoundKick()
			//}
		}
		public static function btnNapAG_mc_Click(evt : MouseEvent) {
			//Thu mo them 1 cua so moi
			var url:String = "http://tala.athena.vn/ZingCredit.aspx?session_id=" + user.SessionId + "&sign_user=" + user.UserId + "&username=" + user.Username;
            var request:URLRequest = new URLRequest(url);
			navigateToURL(request, "");
		}
		
		public static function btnNhanQua_click(evt : MouseEvent) {
			mainStage_mc.Khung_Qua_mc.visible = false;
			var tmpHide:MovieClip = MovieClip(mainStage_mc.getChildByName("Hide"));			
			while (tmpHide != null) {
				mainStage_mc.removeChild(tmpHide) ;
				tmpHide = MovieClip(mainStage_mc.getChildByName("Hide"));
			}
			HCLink();
		}
		
		public static function HCLink() {
			var url:String = "http://hchomecenter.com.vn";
            var request:URLRequest = new URLRequest(url);
			navigateToURL(request, "_blank");
			
		}
		
		public static function btnNew_mc_Click(evt : MouseEvent) {
			mainStage_mc.btnNew.gotoAndStop(2) ;
			mainStage_mc.btnTransferAG.gotoAndStop(1) ;
			mainStage_mc.btnCTDG.gotoAndStop(1) ;
			mainStage_mc.btnCT.gotoAndStop(1) ;
			mainStage_mc.New_mc.visible = true ;
			mainStage_mc.CT_mc.visible = false ;
			mainStage_mc.CTDG_mc.visible = false ;
			mainStage_mc.KhungChuyenAG.visible = false ;	
				mainStage_mc.BGTop_mc.visible = false;
		}
		public static function btnCTDG_mc_Click(evt : MouseEvent) {
			mainStage_mc.btnTransferAG.gotoAndStop(1) ;
			mainStage_mc.btnNew.gotoAndStop(1) ;			
			mainStage_mc.KhungChuyenAG.visible = false ;			
			mainStage_mc.New_mc.visible = false ;
			InitCTDG() ;
		}
		
		public static function btnCT_mc_Click(evt : MouseEvent) {
			mainStage_mc.btnTransferAG.gotoAndStop(1) ;
			mainStage_mc.CT_mc.CTTrongngay_btn.visible = true;
			mainStage_mc.CT_mc.CTHomtruoc_btn.visible = true;
			mainStage_mc.CT_mc.CTTuan_btn.visible = true;
			mainStage_mc.CT_mc.CTThang_btn.visible = true;
			mainStage_mc.btnNew.gotoAndStop(1) ;			
			mainStage_mc.KhungChuyenAG.visible = false ;			
			mainStage_mc.New_mc.visible = false ;
				var Yesterday:Date = new Date();			
			Yesterday.setDate(Yesterday.getDate() - 1);
			var day1: String = Yesterday.getDate().toString();
			var month1 = (Yesterday.getMonth() + 1).toString()
			mainStage_mc.CT_mc.CTHomtruoc_mc.Title_txt.text = "Top Cao thủ Tá lả ngày " + day1 + "/" + month1; 
			InitCTDG_CT() ;
		}
		
		public static function CTTrongngay_btn_Click(evt : MouseEvent) {
			mainStage_mc.CT_mc.CTTrongngay_mc.visible = true ;
			mainStage_mc.CT_mc.CTTuanThang_mc.visible = false ;
			mainStage_mc.CT_mc.CTThang_mc.visible = false ;
			mainStage_mc.CT_mc.CTHomtruoc_mc.visible = false ;
			//mainStage_mc.CT_mc.CTTuanThang_mc.visible = false ;
			mainStage_mc.CT_mc.CTTrongngay_btn.gotoAndStop(2) ;
			mainStage_mc.CT_mc.CTHomtruoc_btn.gotoAndStop(1) ;
			mainStage_mc.CT_mc.CTTuan_btn.gotoAndStop(1) ;
			mainStage_mc.CT_mc.CTThang_btn.gotoAndStop(1) ;
			mainStage_mc.CT_mc.CTTrongngay_mc.Title_txt.text = "Top Cao thủ Tá lả trong ngày" ;
			DisplayTopCT_Trongngay(arrCT_Trongngay, 0) ;
		}
		public static function CTHomtruoc_btn_Click(evt : MouseEvent) {
			mainStage_mc.CT_mc.CTTrongngay_mc.visible = false ;
			mainStage_mc.CT_mc.CTHomtruoc_mc.visible = true ;
			mainStage_mc.CT_mc.CTTuanThang_mc.visible = false ;
			mainStage_mc.CT_mc.CTThang_mc.visible = false ;
			mainStage_mc.CT_mc.CTTrongngay_btn.gotoAndStop(1) ;
			mainStage_mc.CT_mc.CTHomtruoc_btn.gotoAndStop(2) ;
			mainStage_mc.CT_mc.CTTuan_btn.gotoAndStop(1) ;
			mainStage_mc.CT_mc.CTThang_btn.gotoAndStop(1) ;
		
			var Yesterday:Date = new Date();			
			Yesterday.setDate(Yesterday.getDate() - 1);
			var day1: String = Yesterday.getDate().toString();
			var month1 = (Yesterday.getMonth() + 1).toString()
			mainStage_mc.CT_mc.CTHomtruoc_mc.Title_txt.text = "Top Cao thủ Tá lả ngày " + day1 + "/" + month1;  //Yesterday.toLocaleDateString(); //Yesterday.getDate().toString() + "/" (Yesterday.getMonth() + 1).toString;//(today - 1).toString() + "/" + (month+ 1).toString();
			DisplayTopCT_Homtruoc(arrCT_Homtruoc) ;
		}
		public static function CTTuan_btn_Click(evt : MouseEvent) {
			tf.appendText("CAo thu tuan");
			mainStage_mc.CT_mc.CTTuanThang_mc.visible = true ;
			mainStage_mc.CT_mc.CTThang_mc.visible = false ;
			mainStage_mc.CT_mc.CTTrongngay_mc.visible = false ;
			mainStage_mc.CT_mc.CTHomtruoc_mc.visible = false ;
			//mainStage_mc.CT_mc..visible = false ;
			mainStage_mc.CT_mc.CTTrongngay_btn.gotoAndStop(1) ;
			mainStage_mc.CT_mc.CTHomtruoc_btn.gotoAndStop(1) ;
			mainStage_mc.CT_mc.CTTuan_btn.gotoAndStop(2) ;
			mainStage_mc.CT_mc.CTThang_btn.gotoAndStop(1) ;
			var dt:Date = new Date();
			var startDate :Date = new Date();
			var endDate :Date = new Date();
			if (dt.day == 0) {
				endDate.setDate(dt.getDate());
				startDate.setDate(endDate.getDate() - 6);
			}
			else if (dt.day == 1) {
				//tf.appendText("day == 1");
				startDate.setDate(dt.getDate());
				endDate.setDate(startDate.getDate() + 6);
				
			}	
			else 
			{
				startDate.setDate(dt.getDate() - dt.day + 1);
				endDate.setDate(startDate.getDate() + 6);
			}
			
			var DateStart:String = (startDate.getDate()).toString();
			var MonStart:String = (startDate.getMonth() + 1).toString();
			
			var DateEnd:String = (endDate.getDate()).toString();
			var MonEnd:String = (endDate.getMonth() + 1).toString();
			tf.appendText("arrlength = " + arrCT_Tuan.length);
			//tf.appendText("== Day = " + (dt.day).toString());
			if ( arrCT_Tuan.length == 0)
			{
				//tf.appendText(TitleCaoThu);
				tf.appendText("\n trong if");
				tf.appendText("\n start =" + DateStart.toString());
				tf.appendText("\n End = " + DateEnd.toString());
				tf.appendText("\n End month = " + MonEnd.toString());
				tf.appendText("Top Cao thủ Tá lả tuần từ " + DateStart + "/" + MonStart + " - " + DateEnd + "/" + MonEnd);
				mainStage_mc.CT_mc.CTTuanThang_mc.Title_txt.text = "Top Cao thủ Tá lả tuần từ " + DateStart + "/" + MonStart + " - " + DateEnd + "/" + MonEnd ; 
				tf.appendText("\n end title");
			}
			else
			{
				tf.appendText("Trong else");
				mainStage_mc.CT_mc.CTTuanThang_mc.Title_txt.text = TitleCaoThu; 
			}	
			//mainStage_mc.CT_mc.CTTuanThang_mc.Title_txt.text = TitleCaoThu ;//"Top cao thủ trong tuần"  ; }
			DisplayTopCT_TuanThang(arrCT_Tuan) ;
			//mainStage_mc.CT_mc.CTTuanThang_mc.Title_txt.text = TitleCaoThu ;//"Top cao thủ trong tuần"  ; }
			//DisplayTopCT_TuanThang(arrCT_Tuan) ;
			//tf.appendText("CAo thu tuan 2");
		}
		public static function CTThang_btn_Click(evt : MouseEvent) {
				tf.appendText("\n CAo thu thang");
			mainStage_mc.CT_mc.CTThang_mc.visible = true ;
			mainStage_mc.CT_mc.CTTuanThang_mc.visible = false ;
			//mainStage_mc.CT_mc.CTThang_mc.visible = false ;
			mainStage_mc.CT_mc.CTTrongngay_mc.visible = false ;
			mainStage_mc.CT_mc.CTHomtruoc_mc.visible = false ;
			mainStage_mc.CT_mc.CTTrongngay_mc.gotoAndStop(1) ;
			mainStage_mc.CT_mc.CTTrongngay_btn.gotoAndStop(1) ;
			mainStage_mc.CT_mc.CTHomtruoc_btn.gotoAndStop(1) ;
			mainStage_mc.CT_mc.CTTuan_btn.gotoAndStop(1) ;
			mainStage_mc.CT_mc.CTThang_btn.gotoAndStop(2) ;
			var LastMonth: Date = new Date();
			var month = (LastMonth.getMonth() + 1).toString();
			mainStage_mc.CT_mc.CTThang_mc.Title_txt.text = "Top Cao thủ Tá lả tháng " + month;
			tf.appendText("\n CAo thu thang 2 1");
			DisplayTopCT_Thang(arrCT_Thang) ;
				tf.appendText("\n CAo thu thang 2");
		}
		
		//public static function DGTrongngay_btn_Click(evt : MouseEvent) {
			//mainStage_mc.CTDG_mc.DGTrongngay_mc.visible = true ;
			//mainStage_mc.CTDG_mc.DGHomtruoc_mc.visible = false ;
			//mainStage_mc.CTDG_mc.DGTrongngay_btn.gotoAndStop(2) ;
			//mainStage_mc.CTDG_mc.DGHomtruoc_btn.gotoAndStop(1) ;
			//mainStage_mc.CTDG_mc.DGTuan_btn.gotoAndStop(1) ;
			//mainStage_mc.CTDG_mc.DGThang_btn.gotoAndStop(1) ;
			//mainStage_mc.CTDG_mc.DGTrongngay_mc.Title_txt.text = "Top đại gia trong ngày" ;
			//DisplayTopDG_Trongngay(arrDG_Trongngay, 0) ;
		//}
		//public static function DGHomtruoc_btn_Click(evt : MouseEvent) {
			//mainStage_mc.CTDG_mc.DGTrongngay_mc.visible = false ;
			//mainStage_mc.CTDG_mc.DGHomtruoc_mc.visible = true ;
			//mainStage_mc.CTDG_mc.DGTrongngay_btn.gotoAndStop(1) ;
			//mainStage_mc.CTDG_mc.DGHomtruoc_btn.gotoAndStop(2) ;
			//mainStage_mc.CTDG_mc.DGTuan_btn.gotoAndStop(1) ;
			//mainStage_mc.CTDG_mc.DGThang_btn.gotoAndStop(1) ;
			//mainStage_mc.CTDG_mc.DGHomtruoc_mc.Title_txt.text = "Top đại gia hôm trước" ;
			//DisplayTopDG_Homtruoc(arrDG_Homtruoc) ;
		//}
		//public static function DGTuan_btn_Click(evt : MouseEvent) {
			//
			//mainStage_mc.CTDG_mc.DGTrongngay_mc.visible = false ;
			//mainStage_mc.CTDG_mc.DGHomtruoc_mc.visible = true ;
			//mainStage_mc.CTDG_mc.DGTrongngay_btn.gotoAndStop(1) ;
			//mainStage_mc.CTDG_mc.DGHomtruoc_btn.gotoAndStop(1) ;
			//mainStage_mc.CTDG_mc.DGTuan_btn.gotoAndStop(2) ;
			//mainStage_mc.CTDG_mc.DGThang_btn.gotoAndStop(1) ;
			//mainStage_mc.CTDG_mc.DGHomtruoc_mc.Title_txt.text = "Top đại gia trong tuần" ;
			//DisplayTopDG_Trongngay(arrDG_Tuan, 1) ;
		//}
		//public static function DGThang_btn_Click(evt : MouseEvent) {
			//mainStage_mc.CTDG_mc.DGTrongngay_mc.visible = false ;
			//mainStage_mc.CTDG_mc.DGHomtruoc_mc.visible =  true ;
			//mainStage_mc.CTDG_mc.DGTrongngay_btn.gotoAndStop(1) ;
			//mainStage_mc.CTDG_mc.DGHomtruoc_btn.gotoAndStop(1) ;
			//mainStage_mc.CTDG_mc.DGTuan_btn.gotoAndStop(1) ;
			//mainStage_mc.CTDG_mc.DGThang_btn.gotoAndStop(2) ;
			//mainStage_mc.CTDG_mc.DGHomtruoc_mc.Title_txt.text = "Top đại gia trong tháng" ;
			//DisplayTopDG_Trongngay(arrDG_Thang, 1) ;
		//}
		
		public static function InitCTDG() : void {
			//tf.appendText("init dg");
			mainStage_mc.BGTop_mc.visible = true;
			mainStage_mc.btnCTDG.gotoAndStop(2) ;
			mainStage_mc.btnCT.gotoAndStop(1) ;
			mainStage_mc.CTDG_mc.visible = true ;
			mainStage_mc.CT_mc.visible = false ;
			//mainStage_mc.CTDG_mc.CTTrongngay_btn.gotoAndStop(2) ;
			//mainStage_mc.CTDG_mc.DGTrongngay_btn.gotoAndStop(2) ;
			//mainStage_mc.CTDG_mc.CTHomtruoc_btn.gotoAndStop(1) ;
			//mainStage_mc.CTDG_mc.CTTuan_btn.gotoAndStop(1) ;
			//mainStage_mc.CTDG_mc.CTThang_btn.gotoAndStop(1) ;
			//mainStage_mc.CTDG_mc.DGHomtruoc_btn.gotoAndStop(1) ;
			//mainStage_mc.CTDG_mc.DGTuan_btn.gotoAndStop(1) ;
			//mainStage_mc.CTDG_mc.DGThang_btn.gotoAndStop(1) ;
			//tf.appendText("\n init dg 2");
			//mainStage_mc.CTDG_mc.CTTrongngay_mc.visible = true ;
			mainStage_mc.CTDG_mc.DGTrongngay_mc.visible = true ;
			mainStage_mc.CTDG_mc.DGTrongngay_mc.Title_txt.text = "Top đại gia Tá lả trong ngày" ;
			//mainStage_mc.CTDG_mc.CTHomtruoc_mc.visible = false ;
			//mainStage_mc.CTDG_mc.DGHomtruoc_mc.visible = false ;
			//mainStage_mc.CTDG_mc.CTTrongngay_btn.addEventListener(MouseEvent.CLICK, CTTrongngay_btn_Click) ;
			//mainStage_mc.CTDG_mc.CTHomtruoc_btn.addEventListener(MouseEvent.CLICK, CTHomtruoc_btn_Click) ;
			//mainStage_mc.CTDG_mc.CTTuan_btn.addEventListener(MouseEvent.CLICK, CTTuan_btn_Click) ;
			//mainStage_mc.CTDG_mc.CTThang_btn.addEventListener(MouseEvent.CLICK, CTThang_btn_Click) ;
			//mainStage_mc.CTDG_mc.DGTrongngay_btn.addEventListener(MouseEvent.CLICK, DGTrongngay_btn_Click) ;
			//mainStage_mc.CTDG_mc.DGHomtruoc_btn.addEventListener(MouseEvent.CLICK, DGHomtruoc_btn_Click) ;
			//mainStage_mc.CTDG_mc.DGTuan_btn.addEventListener(MouseEvent.CLICK, DGTuan_btn_Click) ;
			//mainStage_mc.CTDG_mc.DGThang_btn.addEventListener(MouseEvent.CLICK, DGThang_btn_Click) ;
			DisplayTopDG_Trongngay(arrDG_Trongngay, 0) ;
			//DisplayTopCT_Trongngay(arrCT_Trongngay, 0) ;
			//tf.appendText("\n Trong ngay");
		}
		
		public static function InitCTDG_CT() : void {
			mainStage_mc.BGTop_mc.visible = true;
			mainStage_mc.btnCTDG.gotoAndStop(1) ;
			mainStage_mc.btnCT.gotoAndStop(2) ;
			mainStage_mc.CT_mc.visible = true ;
			mainStage_mc.CTDG_mc.visible = false ;
			mainStage_mc.CT_mc.CTTuanThang_mc.visible = false ;
			mainStage_mc.CT_mc.CTThang_mc.visible = false ;
			mainStage_mc.CT_mc.CTTrongngay_btn.gotoAndStop(1) ;
			//mainStage_mc.CTDG_mc.DGTrongngay_btn.gotoAndStop(2) ;
			mainStage_mc.CT_mc.CTHomtruoc_btn.gotoAndStop(2) ;
			mainStage_mc.CT_mc.CTTuan_btn.gotoAndStop(1) ;
			mainStage_mc.CT_mc.CTThang_btn.gotoAndStop(1) ;
			//mainStage_mc.CTDG_mc.DGHomtruoc_btn.gotoAndStop(1) ;
			//mainStage_mc.CTDG_mc.DGTuan_btn.gotoAndStop(1) ;
			//mainStage_mc.CTDG_mc.DGThang_btn.gotoAndStop(1) ;
			mainStage_mc.CT_mc.CTTrongngay_mc.visible = false ;
			//mainStage_mc.CTDG_mc.DGTrongngay_mc.visible = true ;
			mainStage_mc.CT_mc.CTHomtruoc_mc.visible = true ;
			//mainStage_mc.CTDG_mc.DGHomtruoc_mc.visible = false ;
			mainStage_mc.CT_mc.CTTrongngay_btn.addEventListener(MouseEvent.CLICK, CTTrongngay_btn_Click) ;
			mainStage_mc.CT_mc.CTHomtruoc_btn.addEventListener(MouseEvent.CLICK, CTHomtruoc_btn_Click) ;
			mainStage_mc.CT_mc.CTTuan_btn.addEventListener(MouseEvent.CLICK, CTTuan_btn_Click) ;
			mainStage_mc.CT_mc.CTThang_btn.addEventListener(MouseEvent.CLICK, CTThang_btn_Click) ;
			//mainStage_mc.CTDG_mc.DGTrongngay_btn.addEventListener(MouseEvent.CLICK, DGTrongngay_btn_Click) ;
			//mainStage_mc.CTDG_mc.DGHomtruoc_btn.addEventListener(MouseEvent.CLICK, DGHomtruoc_btn_Click) ;
			//mainStage_mc.CTDG_mc.DGTuan_btn.addEventListener(MouseEvent.CLICK, DGTuan_btn_Click) ;
			//mainStage_mc.CTDG_mc.DGThang_btn.addEventListener(MouseEvent.CLICK, DGThang_btn_Click) ;
			//DisplayTopDG_Trongngay(arrDG_Trongngay, 0) ;
			
			DisplayTopCT_Homtruoc(arrCT_Homtruoc) ;
		}
		
		public static function QuaTang_fromServer(str: String): void {
			//var tmpHide:MovieClip = MovieClip(new Hide1());
			//tmpHide.name = "Hide";
			//tmpHide.x = 0 ;
			//tmpHide.y = 0 ;
			//tmpHide.alpha = 0 ;
			//mainStage_mc.addChild(tmpHide) ;
			//tf.appendText("str = " + str);
			if (str == null) {
				mainStage_mc.Khung_Qua_mc.visible = false;
				return;
			}
			var objQT: Object = JSON.decode(str);		
			//var objQT :Object = new Object();
			//objQT.PromMoney = 200;
			//objQT.DayPromotion = 2;
			//var numAG:String = objQT.Money.toString();
			//var numDay: String = objQT.Day.toString();
			mainStage_mc.Khung_Qua_mc.visible = true;
			var numAG:String = objQT.PromMoney.toString();
			var numDay: String = objQT.DayPromotion.toString();
			//tf.appendText(numAG + " & " + numDay);
			currPlayer.Mark += int(numAG);
			mainStage_mc.Mark_txt.text = numberFormat(currPlayer.Mark, 0, false, true) + " AG";		
			var formatText : TextFormat = new TextFormat() ;				
			formatText.font = "Tahoma" ;
			formatText.bold = true ;
			formatText.size = 16 ;
			
			mainStage_mc.Mark_txt.setTextFormat(formatText) ;	
				
			if (objQT.DayPromotion == 1)
			{
				mainStage_mc.Khung_Qua_mc.btn_Ngay1_mc.gotoAndStop(2);
				mainStage_mc.Khung_Qua_mc.btn_Ngay1_mc.Ngay_txt.text = numAG  + " AG";
				mainStage_mc.Khung_Qua_mc.btn_Ngay2_mc.gotoAndStop(3);
				mainStage_mc.Khung_Qua_mc.btn_Ngay2_mc.Ngay_txt.text = "200 AG";
				mainStage_mc.Khung_Qua_mc.btn_Ngay3_mc.gotoAndStop(3);
				mainStage_mc.Khung_Qua_mc.btn_Ngay3_mc.Ngay_txt.text = "300 AG";
				mainStage_mc.Khung_Qua_mc.btn_Ngay4_mc.gotoAndStop(3);
				mainStage_mc.Khung_Qua_mc.btn_Ngay4_mc.Ngay_txt.text = "400 AG";
				mainStage_mc.Khung_Qua_mc.btn_Ngay5_mc.gotoAndStop(3);
				mainStage_mc.Khung_Qua_mc.btn_Ngay5_mc.Ngay_txt.text = "500 AG";
				mainStage_mc.Khung_Qua_mc.btn_Ngay6_mc.gotoAndStop(1);
			} else if (objQT.DayPromotion == 2)
			{
				mainStage_mc.Khung_Qua_mc.btn_Ngay1_mc.gotoAndStop(1);
				mainStage_mc.Khung_Qua_mc.btn_Ngay2_mc.gotoAndStop(2);
				mainStage_mc.Khung_Qua_mc.btn_Ngay2_mc.Ngay_txt.text = numAG +" AG";
				mainStage_mc.Khung_Qua_mc.btn_Ngay3_mc.gotoAndStop(3);
				mainStage_mc.Khung_Qua_mc.btn_Ngay3_mc.Ngay_txt.text = "300 AG";
				mainStage_mc.Khung_Qua_mc.btn_Ngay4_mc.gotoAndStop(3);
				mainStage_mc.Khung_Qua_mc.btn_Ngay4_mc.Ngay_txt.text = "400 AG";
				mainStage_mc.Khung_Qua_mc.btn_Ngay5_mc.gotoAndStop(3);
				mainStage_mc.Khung_Qua_mc.btn_Ngay5_mc.Ngay_txt.text = "500 AG";
				mainStage_mc.Khung_Qua_mc.btn_Ngay6_mc.gotoAndStop(1);
			}else if (objQT.DayPromotion == 3)
			{
				mainStage_mc.Khung_Qua_mc.btn_Ngay1_mc.gotoAndStop(1);
				mainStage_mc.Khung_Qua_mc.btn_Ngay2_mc.gotoAndStop(1);
				mainStage_mc.Khung_Qua_mc.btn_Ngay3_mc.gotoAndStop(2);
				mainStage_mc.Khung_Qua_mc.btn_Ngay3_mc.Ngay_txt.text =  numAG + " AG";
				mainStage_mc.Khung_Qua_mc.btn_Ngay4_mc.gotoAndStop(3);
				mainStage_mc.Khung_Qua_mc.btn_Ngay4_mc.Ngay_txt.text = "400 AG";
				mainStage_mc.Khung_Qua_mc.btn_Ngay5_mc.gotoAndStop(3);
				mainStage_mc.Khung_Qua_mc.btn_Ngay5_mc.Ngay_txt.text = "500 AG";
				mainStage_mc.Khung_Qua_mc.btn_Ngay6_mc.gotoAndStop(1);
			}else if (objQT.DayPromotion == 4)
			{
				mainStage_mc.Khung_Qua_mc.btn_Ngay1_mc.gotoAndStop(1);
				mainStage_mc.Khung_Qua_mc.btn_Ngay2_mc.gotoAndStop(1);
				mainStage_mc.Khung_Qua_mc.btn_Ngay3_mc.gotoAndStop(1);
				mainStage_mc.Khung_Qua_mc.btn_Ngay4_mc.gotoAndStop(2);
				mainStage_mc.Khung_Qua_mc.btn_Ngay4_mc.Ngay_txt.text = numAG + " AG";
				mainStage_mc.Khung_Qua_mc.btn_Ngay5_mc.gotoAndStop(3);
				mainStage_mc.Khung_Qua_mc.btn_Ngay5_mc.Ngay_txt.text = "500 AG";
				mainStage_mc.Khung_Qua_mc.btn_Ngay6_mc.gotoAndStop(1);
			}else if (objQT.DayPromotion == 5)
			{
				mainStage_mc.Khung_Qua_mc.btn_Ngay1_mc.gotoAndStop(1);
				mainStage_mc.Khung_Qua_mc.btn_Ngay2_mc.gotoAndStop(1);
				mainStage_mc.Khung_Qua_mc.btn_Ngay3_mc.gotoAndStop(1);
				mainStage_mc.Khung_Qua_mc.btn_Ngay4_mc.gotoAndStop(1);
				mainStage_mc.Khung_Qua_mc.btn_Ngay5_mc.gotoAndStop(2);
				mainStage_mc.Khung_Qua_mc.btn_Ngay5_mc.Ngay_txt.text = numAG + " AG";
				mainStage_mc.Khung_Qua_mc.btn_Ngay6_mc.gotoAndStop(1);
			}else if (objQT.DayPromotion == 6)
			{
				mainStage_mc.Khung_Qua_mc.btn_Ngay1_mc.gotoAndStop(1);
				mainStage_mc.Khung_Qua_mc.btn_Ngay2_mc.gotoAndStop(1);
				mainStage_mc.Khung_Qua_mc.btn_Ngay3_mc.gotoAndStop(1);
				mainStage_mc.Khung_Qua_mc.btn_Ngay4_mc.gotoAndStop(1);
				mainStage_mc.Khung_Qua_mc.btn_Ngay5_mc.gotoAndStop(1);
				mainStage_mc.Khung_Qua_mc.btn_Ngay6_mc.gotoAndStop(1);
				//mainStage_mc.Khung_Qua_mc.btn_Ngay6_mc.Ngay6_txt.text = numAG + " AG";
			}
			mainStage_mc.Khung_Qua_mc.Intro_mc.NumDay_txt.text = numAG + " AG";
			
			mainStage_mc.Khung_Qua_mc.btn_NhanQua.addEventListener(MouseEvent.CLICK, btnNhanQua_click);
			
		}
		
		//public static function MCLoad() {		
			//tf.appendText("MC LOAD");
			//if (currPlayer.Level == 2)  return;
			//imageLoaderMain1 = new Loader();
			//imageMain1 = new URLRequest(preURLImage + "Khung_Tin.swf");
			//tf.appendText(imageMain1.url.toString());
			//tf.appendText("\n MC LOAD 1");
			//imageLoaderMain1.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoader);				
			//imageLoaderMain1.name = "MC" ;
			//imageLoaderMain1.load(imageMain1);			
			//function onLoader(evt:Event)
			//{	
				//tf.appendText("\n MC LOAD onload 1");
				//var tmpHide:MovieClip = MovieClip(new Hide());
				//tmpHide.name = "Hide";
				//tmpHide.x = 0 ;
				//tmpHide.y = 0 ;
				//tmpHide.alpha = 0 ;
				//mainStage_mc.addChild(tmpHide) ;
				//imageLoaderMain1.x = 0 ;
				//imageLoaderMain1.y = 0 ; 
				//arrQC1.push(imageLoaderMain1) ;	
				//tf.appendText("\n MC LOAD onload 2");
				//if ((mainStage_mc.currentFrame == 3))  {
					//if (mainStage_mc.BGNew_mc.numChildren > 1) {	
						//mainStage_mc.BGNew_mc.removeChildAt(1) ;
					//}								
					//mainStage_mc.BGNew_mc.addChild(arrQC1[0]);
				//}
				//imageLoaderMain = null ;
			//}	
			//tf.appendText("\n MC LOAD onload 3");
		//}		
		//
		// end khung qua tnag
		
		public static function TutLoad() {		
			//if (currPlayer.Level == 2)  return;
			imageLoaderMain = new Loader();
			imageMain = new URLRequest(preURLImage + "TutTala.swf?User=" + currPlayer.PlayerName + "&Level=" +currPlayer.Level);
			
			imageLoaderMain.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoader);				
			imageLoaderMain.name = "HC" ;
			imageLoaderMain.load(imageMain);			
			function onLoader(evt:Event)
			{	
				//var tmpHide:MovieClip = MovieClip(new Hide());
				//tmpHide.name = "Hide";
				//tmpHide.x = 0 ;
				//tmpHide.y = 0 ;
				//tmpHide.alpha = 0 ;
				//mainStage_mc.addChild(tmpHide) ;
				imageLoaderMain.x = 0 ;
				imageLoaderMain.y = 0 ; 
				arrQC.push(imageLoaderMain) ;	
				if ((mainStage_mc.currentFrame == 3))  {
					if (mainStage_mc.Test_mc.numChildren > 1) {	
						mainStage_mc.Test_mc.removeChildAt(1) ;
					}								
					mainStage_mc.Test_mc.addChild(arrQC[0]);
				}
				imageLoaderMain = null ;
			}	
		}		
		
		
		public static function initLevelRoom(str:String):void {		
			//tf.appendText("\n Test");
			var object :Array  = (JSON.decode(str) as Array) ;		
			//tf.appendText("\n str = " + str);
			mainStage_mc.gotoAndStop(3) ;
			//{Load BackGround
			if (mainStage_mc.BG_mc.numChildren > 1)
				mainStage_mc.BG_mc.removeChildAt(1) ;			
			if (mainStage_mc.BGBanbe_mc.numChildren > 1)
				mainStage_mc.BGBanbe_mc.removeChildAt(1) ;
			if (mainStage_mc.BGCT_mc.numChildren > 1)
				mainStage_mc.BGCT_mc.removeChildAt(1) ;
			if (mainStage_mc.BGNew_mc.numChildren > 1)
				mainStage_mc.BGNew_mc.removeChildAt(1) ;
			if (mainStage_mc.BGLogo_mc.numChildren > 1)
				mainStage_mc.BGLogo_mc.removeChildAt(1) ;			
			if (mainStage_mc.BGTop_mc.numChildren > 1)
				mainStage_mc.BGTop_mc.removeChildAt(1) ;
			if (ArrBG[0] != null)
				mainStage_mc.BG_mc.addChild(ArrBG[0]) ;
			if (ArrBG[1] != null)
				mainStage_mc.BGLogo_mc.addChild(ArrBG[1]) ;			
			if (ArrBG[2] != null) 
				mainStage_mc.BGBanbe_mc.addChild(ArrBG[2]) ;
			if (ArrBG[3] != null)
				mainStage_mc.BGCT_mc.addChild(ArrBG[3]) ;
				//MCLoad();
			if (ArrBG[4] != null)
				mainStage_mc.BGNew_mc.addChild(ArrBG[4]) ;	
			if (ArrBG[5] != null)
				mainStage_mc.BGTop_mc.addChild(ArrBG[5]) ;	
			//}
			//{Hien thi thong tin nguoi choi
		
			if ((currPlayer.TinyURL != "") && (currPlayer.TinyURL != null)) {
				var imageLoader:Loader = new Loader();
				var image:URLRequest = new URLRequest(currPlayer.TinyURL) ;
				imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoader);				
				imageLoader.name = "BigAW" ;
				imageLoader.load(image);				
				function onLoader(evt:Event)
				{
					imageLoader.width = 50 ;
					imageLoader.height = 50 ;
					if (mainStage_mc.Avatar_mc.numChildren > 1)
						mainStage_mc.Avatar_mc.removeChildAt(1) ;
					mainStage_mc.Avatar_mc.addChild(imageLoader);
				}				
			}
			
			mainStage_mc.Name_txt.text = currPlayer.PlayerName ;
			mainStage_mc.Mark_txt.text = numberFormat(currPlayer.Mark, 0, false, true) + " AG";		
			InitMoney = currPlayer.Mark;
			var formatText : TextFormat = new TextFormat() ;				
			formatText.font = "Tahoma" ;
			formatText.bold = true ;
			formatText.size = 16 ;
			mainStage_mc.Name_txt.setTextFormat(formatText) ;
			mainStage_mc.Mark_txt.setTextFormat(formatText) ;	
		// Khung qua tang
		//tf.appendText("iniot load");
		mainStage_mc.Khung_Qua_mc.visible = false;
				//TutLoad();
		//tf.appendText("\n iniot load 2");	
			//tf.appendText("\n Khung qua invi");
			mainStage_mc.btnTransferAG.addEventListener(MouseEvent.CLICK, btnTransferAG_mc_Click) ;
			mainStage_mc.btnCTDG.addEventListener(MouseEvent.CLICK, btnCTDG_mc_Click) ;
			mainStage_mc.btnCT.addEventListener(MouseEvent.CLICK, btnCT_mc_Click) ;
			mainStage_mc.btnNew.addEventListener(MouseEvent.CLICK, btnNew_mc_Click) ;
			mainStage_mc.btnNapAG.addEventListener(MouseEvent.CLICK, btnNapAG_mc_Click) ;	
			//tf.appendText("\n truoc khsi check date");
						//InitCTDG() ;
			var dtCheck : Date = new Date() ;
			if (dtCheck.day == 1 || dtCheck.date ==1)
			{
				//tf.appendText("\n Trong if vs date = 1");
					mainStage_mc.CTDG_mc.visible = false ;
				//mainStage_mc.CTDG_mc.DGTrongngay_mc.visible = false ;
					mainStage_mc.CT_mc.visible = false ;
					mainStage_mc.CT_mc.CTTuanThang_mc.visible = false ;
					mainStage_mc.CT_mc.CTThang_mc.visible = false ;
					mainStage_mc.CT_mc.CTTrongngay_mc.visible = false ;
					mainStage_mc.CT_mc.CTHomtruoc_mc.visible = false ;
					mainStage_mc.btnCT.gotoAndStop(1) ;
					mainStage_mc.CT_mc.CTTrongngay_btn.visible = false;
					mainStage_mc.CT_mc.CTHomtruoc_btn.visible = false;
					mainStage_mc.CT_mc.CTTuan_btn.visible = false;
					mainStage_mc.CT_mc.CTThang_btn.visible = false;
					mainStage_mc.btnCTDG.gotoAndStop(1) ;
					mainStage_mc.btnTransferAG.gotoAndStop(1) ;
					mainStage_mc.btnNew.gotoAndStop(2) ;
					mainStage_mc.KhungChuyenAG.visible = false ;
					mainStage_mc.New_mc.visible = true ;
					mainStage_mc.BGTop_mc.visible = false;
			} else {
				var Yesterday:Date = new Date();			
				Yesterday.setDate(Yesterday.getDate() - 1);
				var day1: String = Yesterday.getDate().toString();
				var month1 = (Yesterday.getMonth() + 1).toString()
				mainStage_mc.CT_mc.CTHomtruoc_mc.Title_txt.text = "Top Cao thủ Tá lả ngày " + day1 + "/" + month1;  
				
				mainStage_mc.CTDG_mc.visible = false ;
				//mainStage_mc.CTDG_mc.DGTrongngay_mc.visible = false ;
				mainStage_mc.CT_mc.visible = true ;
				mainStage_mc.CT_mc.CTTuanThang_mc.visible = false ;
				mainStage_mc.CT_mc.CTThang_mc.visible = false ;
				mainStage_mc.btnCT.gotoAndStop(2) ;
				mainStage_mc.CT_mc.CTTrongngay_btn.visible = false;
				mainStage_mc.CT_mc.CTHomtruoc_btn.visible = false;
				mainStage_mc.CT_mc.CTTuan_btn.visible = false;
				mainStage_mc.CT_mc.CTThang_btn.visible = false;
				mainStage_mc.btnCTDG.gotoAndStop(1) ;
				mainStage_mc.btnTransferAG.gotoAndStop(1) ;
				mainStage_mc.btnNew.gotoAndStop(1) ;
				mainStage_mc.KhungChuyenAG.visible = false ;
				mainStage_mc.New_mc.visible = false ;
				InitCTDG_CT();
			}
			for (var i:int = 0; i < object.length; i++) 
			{
				var item:Object	 = object[i];
				var oLR : ObjectLevelRoom = new ObjectLevelRoom() ;
				oLR.LevelID = item.Id ;
				oLR.LevelName = item.Name ;
				oLR.CurrPlayer = item.CurPlay ;
				oLR.MaxMark = item.MaxMark ;
				oLR.MinMark = item.MinMark ;
				arrLevelInfor.push(item) ;
				if (i == 0) {
					new LevelRoomController(mainStage_mc.level1_mc, oLR) ;
				} else if (i == 1) {
					new LevelRoomController(mainStage_mc.level2_mc, oLR) ;
				}
			}			
			movieRooms = new Array() ;
			//{ Kiem tra gui yeu cau Feed len tuong
			var markChange : Number = 0 ;
			var markS : Number ;
			LoadData() ;
			markInit = currPlayer.Mark ;
			ExternalInterface.call("pushfeed", user.UserId,1);
			//tf.appendText("Feed = " +user.UserId + "game = Tala" );
			//}
		}
		
		public static function DisplayContactFriends(str : String) {
			var tmpHide:MovieClip = MovieClip(new Hide());
			tmpHide.name = "Hide";
			tmpHide.x = 0 ;
			tmpHide.y = 0 ;
			tmpHide.alpha = 0 ;
			mainStage_mc.addChild(tmpHide) ;			
			var tmpTable:MovieClip = MovieClip(new Popup_Khoebanbe());
			if (ArrPopupBG[2] != null)
				tmpTable.BG_mc.addChild(ArrPopupBG[2]) ;
			tmpTable.name = "Popup_Khoebanbe";
			tmpTable.x = 256;
			tmpTable.y = 100;
			tmpTable.txtName.text = user.Username ;
			tmpTable.txtMark.text = "Đang có " + numberFormat(currPlayer.Mark, 0, false, true) + " AG!";
			tmpTable.txtInput.text = str ;
			if (currPlayer.TinyURL != null) 
				if (currPlayer.TinyURL != "") {
					var imageLoader:Loader = new Loader();
					var image:URLRequest = new URLRequest(currPlayer.TinyURL) ;					
					imageLoader = new Loader();
					imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoader);				
					imageLoader.name = "TinyAW" ;
					imageLoader.load(image);					
					function onLoader(evt:Event)
					{
						imageLoader.width = 50 ;
						imageLoader.height = 50 ;
						tmpTable.Avatar_mc.addChild(imageLoader);
					}					
				}
			tmpTable.addEventListener(KeyboardEvent.KEY_DOWN, keyPopupTableFriends) ;			
			tmpTable.btnOK_mc.addEventListener(MouseEvent.CLICK, btnOK_Friends_click);			
			tmpTable.btnNotOK_mc.addEventListener(MouseEvent.CLICK, btnNotOK_Friends_click);		
			mainStage_mc.addChild(tmpTable);
		}
		public static function keyPopupTableFriends(evt : KeyboardEvent) { //Xu ly Phim tren popup tao ban
			if (evt.keyCode == 13) {				
				var tmp:MovieClip = MovieClip(mainStage_mc.getChildByName("Popup_Khoebanbe")) ;
				var strName : String = tmp.txtInput.text ;		
				var url:String = preURLFeed;
				var request:URLRequest = new URLRequest(url);
				request.method = URLRequestMethod.GET;
				var variables:URLVariables = new URLVariables();
				variables.sessionkey = user.SessionId ;// user.SessionId;//1F0189889788043E975D3ECD
				variables.feedmessage = strName;
				variables.money = numberFormat(currPlayer.Mark) ;
				variables.disname = user.Username ;
				request.data = variables;
				sendToURL(request);
				mainStage_mc.removeChild(tmp) ;
				var tmpHide:MovieClip = MovieClip(mainStage_mc.getChildByName("Hide"));			
				while (tmpHide != null) {
					mainStage_mc.removeChild(tmpHide) ;
					tmpHide = MovieClip(mainStage_mc.getChildByName("Hide"));
				}
			}			
		}		
		public static function btnOK_Friends_click(evt:MouseEvent) { //Xu ly Kick Xac nhan tren popup tao ban
			var tmp:MovieClip = MovieClip(mainStage_mc.getChildByName("Popup_Khoebanbe")) ;
			var strName : String = tmp.txtInput.text ;		
			
			var url:String = preURLFeed;
            var request:URLRequest = new URLRequest(url);
			request.method = URLRequestMethod.GET;
            var variables:URLVariables = new URLVariables();
            variables.sessionkey = user.SessionId ;// user.SessionId;//1F0189889788043E975D3ECD
            variables.feedmessage = strName;
			variables.money = numberFormat(currPlayer.Mark) ;
			variables.disname = user.Username ;
            request.data = variables;
			sendToURL(request);			
			mainStage_mc.removeChild(tmp) ;
			var tmpHide:MovieClip = MovieClip(mainStage_mc.getChildByName("Hide"));			
			while (tmpHide != null) {
				mainStage_mc.removeChild(tmpHide) ;
				tmpHide = MovieClip(mainStage_mc.getChildByName("Hide"));
			}			
			//{ Tao Tieng
			SoundKick()
			//}
		}
		public static function btnNotOK_Friends_click(evt:MouseEvent) { //Xu ly Kick Huy bo tren popup tao ban
			var tmp :MovieClip = MovieClip(mainStage_mc.getChildByName("Popup_Khoebanbe")) ;
			mainStage_mc.removeChild(tmp) ;
			var tmpHide:MovieClip = MovieClip(mainStage_mc.getChildByName("Hide"));			
			while (tmpHide != null) {
				mainStage_mc.removeChild(tmpHide) ;
				tmpHide = MovieClip(mainStage_mc.getChildByName("Hide"));
			}
			//{ Tao Tieng
			SoundKick()
			//}
		}
		
		public static function clickLevel(_id:int, maxAG : int, minAG : int):void {
			//{ Tao Tieng
			SoundKick()
			//}
			if ((minAG == 0) && (currPlayer.Mark > 20000)) {
				showMSG("Bạn đã vượt quá cấp độ này!") ;
				return ;
			} else if ((minAG == 5000) && (currPlayer.Mark < 5000)) {
				showMSG("Bạn chưa đủ AG chơi cấp độ này!") ;
				return ;
			}/* else if ((minAG == 50000) && (currPlayer.Mark < 50000)) {
				showMSG("Bạn chưa đủ AG chơi cấp độ này!") ;
				return ;
			}*/
			strTemp = "SL:" + "Level:" + _id + "ID:" + user.PlayId;
			levelID = minAG ;
			mainSocket.sendData(strTemp);		
			//Remove Tang diem neu co
			var tmp :MovieClip = MovieClip(mainStage_mc.getChildByName("Thongbao")) ;
			while (tmp != null) {
				mainStage_mc.removeChild(tmp) ;
				var tmpHide:MovieClip = MovieClip(mainStage_mc.getChildByName("Hide"));
				while (tmpHide != null) {
					mainStage_mc.removeChild(tmpHide) ;
					tmpHide = MovieClip(mainStage_mc.getChildByName("Hide"));
				}
				tmp = MovieClip(mainStage_mc.getChildByName("Thongbao")) ;
			}	
		}
		
		public static function DisplayHistory(str : String) : void {	
			//tf.appendText("His:" + str) ;
			var obj = JSON.decode(str) ;
			var _mc :MovieClip = MovieClip(new Player_TransferAG()) ;	
			_mc.txtFrom.text = obj.FromUser ;
			_mc.txtAG.text = numberFormat(obj.AG, 0, false, true) ;
			_mc.txtTime.text = obj.WaitTime ;
			_mc.x = 0 ; 
			_mc.y = 5 + movieHistoryTF.length * 22 ;
			currPlayer.Mark = currPlayer.Mark + obj.AG ;
			if (mainStage_mc.currentFrame == 3) {
				var formatText : TextFormat = new TextFormat() ;				
				formatText.font = "Tahoma" ;
				formatText.bold = true ;
				formatText.size = 12 ;
				mainStage_mc.Mark_txt.text = numberFormat(currPlayer.Mark, 0, false, true) + " AG";
				mainStage_mc.Mark_txt.setTextFormat(formatText) ;
			}
			movieHistoryTF.push(_mc) ;			
			var pp : MovieClip = MovieClip(mainStage_mc.getChildByName("Popup_History")) ;
			if (pp == null) {
				var tmpHide:MovieClip = MovieClip(new Hide());
				tmpHide.name = "Hide";
				tmpHide.x = 0 ;
				tmpHide.y = 0 ;
				tmpHide.alpha = 0 ;
				mainStage_mc.addChild(tmpHide) ;
				pp = MovieClip(new Popup_History());
				if (ArrPopupBG[2] != null) {
					pp.BG_mc.addChild(ArrPopupBG[2]) ;
					pp.BG_mc.width = 400 ;
					pp.BG_mc.height = 286 ;
				}
				pp.name = "Popup_History";
				pp.x = 186;
				pp.y = 100;				
				pp.btnXacnhan_mc.addEventListener(MouseEvent.CLICK, btnXacnhanHistory_mc_click);
				scrollHistoryTF_mc = new MovieClip() ;
				scrHistoryTF.source = scrollHistoryTF_mc;
				scrHistoryTF.setSize(370, 150); 
				scrHistoryTF.horizontalScrollPolicy = ScrollPolicy.OFF ;
				scrHistoryTF.verticalScrollPolicy = ScrollPolicy.ON ;
				scrHistoryTF.move( 15, 45);
				pp.addChild(scrHistoryTF) ;
				mainStage_mc.addChild(pp) ;
			}
			scrollHistoryTF_mc.addChild(_mc) ;
			scrHistoryTF.update() ;
		}
		
		public static function btnXacnhanHistory_mc_click(evt:MouseEvent) {
			//{ Tao Tieng
			SoundKick()
			//}
			var tmp :MovieClip = MovieClip(mainStage_mc.getChildByName("Popup_History")) ;
			mainStage_mc.removeChild(tmp) ;
			var tmpHide:MovieClip = MovieClip(mainStage_mc.getChildByName("Hide"));
			while (tmpHide != null) {
				mainStage_mc.removeChild(tmpHide) ;
				tmpHide = MovieClip(mainStage_mc.getChildByName("Hide"));
			}
		}
		/******************************************************************************************************/
		/******************************** Functions in Frame 4 ************************************************/
		/******************************************************************************************************/
		public static function TaoKhungFrame2():void { //Khoi tao hien thi cac element thuoc Frame 4
			mainStage_mc.gotoAndStop(4);
			if (mainStage_mc.BG_mc.numChildren > 1)
				mainStage_mc.BG_mc.removeChildAt(1) ;
			if (mainStage_mc.BGListTable_mc.numChildren > 1)
				mainStage_mc.BGListTable_mc.removeChildAt(1) ;
			if (ArrBG[0] != null)
				mainStage_mc.BG_mc.addChild(ArrBG[0]) ;
			if (ArrBG[6] != null) 
				mainStage_mc.BGListTable_mc.addChild(ArrBG[6]) ;
			//{Hien thi Button
			mainStage_mc.btnCreateTable.addEventListener(MouseEvent.CLICK, clickCreateTable) ;			
			mainStage_mc.btnComeInTable.addEventListener(MouseEvent.CLICK, clickComeInTable) ;
			mainStage_mc.btnPlayGame.addEventListener(MouseEvent.CLICK, btnPlayGame) ;			
			mainStage_mc.btnBack_mc.addEventListener(MouseEvent.CLICK, btnBack_mc_Click);
			mainStage_mc.btnKhoeBan_mc.addEventListener(MouseEvent.CLICK, btnKhoeBan_mc_Click);
			if (isBegin) {
				mainStage_mc.btnCreateTable.visible = false ;
				mainStage_mc.btnComeInTable.visible = false ;				
				mainStage_mc.btnPlayGame.visible = false ;
			}			
			//}
			//{ Hien thi thong tin nguoi choi
			if ((currPlayer.TinyURL != "") && (currPlayer.TinyURL != null)) {
				var imageLoader:Loader = new Loader();
				var image:URLRequest = new URLRequest(currPlayer.TinyURL) ;
				imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoader);				
				imageLoader.name = "BigAW" ;
				imageLoader.load(image);
				function onLoader(evt:Event)
				{
					imageLoader.width = 50 ;
					imageLoader.height = 50 ;
					if (mainStage_mc.Avatar_mc.numChildren > 1){
						mainStage_mc.Avatar_mc.removeChildAt(1) ;
					}
					mainStage_mc.Avatar_mc.addChild(imageLoader);
				}				
			}
			mainStage_mc.Name_txt.text = currPlayer.PlayerName ;
			mainStage_mc.Mark_txt.text = numberFormat(currPlayer.Mark, 0, false, true) + " AG" ;			
			var formatText : TextFormat = new TextFormat() ;				
			formatText.font = "Tahoma" ;
			formatText.bold = true ;
			formatText.size = 12 ;
			mainStage_mc.Name_txt.setTextFormat(formatText) ;
			mainStage_mc.Mark_txt.setTextFormat(formatText) ;			
			//}
			//{ Check Tien hien Popup
			if ((currPlayer.Mark < 500) && (numberWarning < 2)) {
				var tmpHide:MovieClip = MovieClip(new Hide());
				tmpHide.name = "Hide";
				tmpHide.x = 0 ;
				tmpHide.y = 0 ;
				tmpHide.alpha = 0 ;
				mainStage_mc.addChild(tmpHide) ;
				numberWarning = numberWarning + 1 ;
				var tmpTB:MovieClip = MovieClip(new Popup_TangAG()) ;
				if (currPlayer.Mark == 0)
					tmpTB.txtThongbao.text = "Bạn đã hết AG, Bạn có muốn nạp AG ?"	 ;
				else 
					tmpTB.txtThongbao.text = "Bạn đã gần hết AG, Bạn có muốn nạp thêm AG ?"	 ;
				tmpTB.NapAG_btn.addEventListener(MouseEvent.CLICK, NapAG_Popup_Click) ;
				tmpTB.Choitiep_btn.addEventListener(MouseEvent.CLICK, Choitiep_Popup_Click) ;
				tmpTB.name = "TangAG" ;
				tmpTB.x = 240;
				tmpTB.y = 220;
				mainStage_mc.addChild(tmpTB) ;
			}
			//}
		}
		
		public static function initRoom(str:String):void { //Add 1 Room vao giao dien
		//tf.appendText(str);
			if (mainStage_mc.currentFrame != 4) {
				movieRooms = new Array() ;
				TaoKhungFrame2();
			}
			if (movieRooms == null) movieRooms = new Array() ;
			var or:ObjectRoom = new ObjectRoom();
			var object = JSON.decode(str) ; 
			if (object == null) return ;
			if (movieRooms.length > 5) return ;
			//if (scrollPaneRoom_mc == null) scrollPaneRoom_mc = new MovieClip() ;
			if (mainStage_mc.ListRoom_mc.getChildByName("Room:" + object.Id) != null) return ;
			or.RoomID = object.Id;
			or.RoomName = object.Name;
			or.LevelID = object.LvlID ;
			or.MaxTable = object.MaxTable ;
			or.CurrPlayer = object.CurPlay ;
			var _mc :MovieClip = MovieClip(new Room()) ;	
			_mc.name = "Room:" + object.Id ;
			_mc.btnBg_mc.gotoAndStop(2);
			_mc.x = movieRooms.length * 150 ;
			_mc.y = 0 ;
			if ((currRoom != null) && (!isBegin)) {
				if (object.Id == currRoom.RoomID)
					_mc.btnBg_mc.gotoAndStop(1) ;				
			}
			movieRooms.push(_mc) ;			
			new RoomController(_mc, or) ;
			mainStage_mc.ListRoom_mc.addChild(_mc) ;
			if (movieRooms.length == 1) 
				clickRoom(object.Id) ;
		}
		
		public static function clickRoom(_id:int):void {
			//tf.appendText("click room 1");
			//{ Tao Tieng
			SoundKick()
			//}
			strTemp = "SL:" + "Room:" + _id + "ID:" + user.PlayId;
			if (currRoom == null) 
				currRoom = new ObjectRoom() ;
			currRoom.RoomID = _id ;	
			roomID = _id ;
			if (isBegin) {
				mainStage_mc.btnCreateTable.visible = true ;
				mainStage_mc.btnComeInTable.visible = true ;
				mainStage_mc.btnPlayGame.visible = true ;
				isBegin = false ;
			}
			//Khoi dong
			movieTables = new Array() ;
			movieTablesXY = new Array() ;
			scrollPaneTable_mc = new MovieClip() ;
			scrTable.source = scrollPaneTable_mc; 
			scrTable.setSize(770, 545); 
			scrTable.horizontalScrollPolicy = ScrollPolicy.OFF ;
			scrTable.verticalScrollPolicy = ScrollPolicy.ON ;
			scrTable.move(0, 0);			
			mainStage_mc.ListTable_mc.addChild(scrTable) ;			
			mainSocket.sendData(strTemp);
			//Khoi tao vien sang
			for (var i:int = 0; i < movieRooms.length; i++) 
			{
				if (movieRooms[i].name == "Room:" + _id)
					movieRooms[i].btnBg_mc.gotoAndStop(1) ;
				else 
					movieRooms[i].btnBg_mc.gotoAndStop(2) ;
			}
			
			//tf.appendText("click room 2");
			//Hide
			/*var tmpHide:MovieClip = MovieClip(new Hide());
			tmpHide.name = "HideCR";
			tmpHide.x = 0 ;
			tmpHide.y = 0 ;
			tmpHide.alpha = 0 ;
			mainStage_mc.addChild(tmpHide) ;
			//Hien thi man hinh Loading
			var tmpLD:MovieClip = MovieClip(new Loading());
			tmpLD.name = "LoadingCR";
			tmpLD.x = 340;
			tmpLD.y = 250;			
			mainStage_mc.addChild(tmpLD);*/
		}
		
		public static function initTable(str:String):void { //Add vao danh sach table mot table tra ve
			//tf.appendText("==>init:" + str) ;
			//Xoa Loading
			/*var tmpHide:MovieClip = MovieClip(mainStage_mc.getChildByName("HideCR"));			
			while (tmpHide != null) {
				mainStage_mc.removeChild(tmpHide) ;
				tmpHide = MovieClip(mainStage_mc.getChildByName("HideCR"));
			}
			var tmpLD:MovieClip = MovieClip(mainStage_mc.getChildByName("LoadingCR"));			
			while (tmpLD != null) {
				mainStage_mc.removeChild(tmpLD) ;
				tmpLD = MovieClip(mainStage_mc.getChildByName("LoadingCR"));
			}*/
			if (str == "0") return ;
			//tf.appendText("==>init1:" + str) ;
			//Xu ly du lieu
			var object = JSON.decode(str) ;
			var ot:ObjectTable = new ObjectTable();
			ot.TableID = object.Id ;
			ot.RoomID = object.RId ;
			ot.CurrentPlayer = 1; 
			ot.TableName = object.Name ;
			ot.MarkUnit = object.MarkUnit ;
			ot.Status = 0 ;
			if (ot.RoomID != roomID) return ;
			if (movieTables == null) movieTables = new Array() ;
			if (movieTablesXY == null) movieTables = new Array() ;
			//Cap nhat User trong ban
			for (var j:int = 0; j < movieTables.length; j++) 
			{
				var _mcUpdateParent : MovieClip = MovieClip(scrollPaneTable_mc.getChildByName("Table:" + ot.TableID)) ;				
				if (_mcUpdateParent != null) {
					var _mcUpdate : MovieClip = MovieClip(_mcUpdateParent.getChildAt(0)) ;
					_mcUpdate.user1_txt.text = "" ;
					_mcUpdate.user2_txt.text = "" ;
					_mcUpdate.user3_txt.text = "" ;
					_mcUpdate.user4_txt.text = "" ;
					for (var k:int = 0; k < object.ArrName.length; k++) 
					{
						if (k == 0)
							_mcUpdate.user1_txt.text = object.ArrName[k] ;
						else if (k == 1)
							_mcUpdate.user2_txt.text = object.ArrName[k] ;
						else if (k == 2)
							_mcUpdate.user3_txt.text = object.ArrName[k] ;
						else if (k == 3)
							_mcUpdate.user4_txt.text = object.ArrName[k] ;
					} 
					return ;
				}				
			}
			var mc:MovieClip = new MovieClip();	
			var tmpTableTemp : MovieClip = MovieClip(new TablePlay());
			var tmpTable : MovieClip = MovieClip(new TablePlay());	
			if (ot.MarkUnit == 20 || ot.MarkUnit == 50 || ot.MarkUnit == 100 || ot.MarkUnit == 200 || ot.MarkUnit == 1000 || ot.MarkUnit == 2000 || ot.MarkUnit == 5000 || ot.MarkUnit == 10000) {
				tmpTableTemp = MovieClip(new TablePlay());
				tmpTable = MovieClip(new TablePlay());
			} else {
				tmpTableTemp = MovieClip(new TableFree());
				tmpTable = MovieClip(new TableFree());
			}
			tmpTableTemp.name = "Table:" + ot.TableID ;
			tmpTable.name = "Table:" + ot.TableID ; //Gan Ten de ty dieu khien
			new TableController(tmpTable, ot, object.ArrName);
			var xNew : Number = 5 ;
			var yNew : Number = 5 ;
			var t : Boolean = true ;
			var isTD : int = 0 ;
			while (t) {
				t = false ;
				for (var i:int = 0; i < movieTablesXY.length; i++) 
				{
					if ((movieTablesXY[i].x == xNew) && (movieTablesXY[i].y == yNew)) {
						t = true ;
						break ;
					}					
				}
				if (t) {
					if ((isTD == 0) || (isTD == 1) || (isTD == 2)) {
						xNew = xNew + 188 ;
						isTD = isTD + 1 ;
					} else {						
						yNew = yNew + 108 ;
						xNew = 5 ;
						isTD = 0
					}
				}
			}
			mc.addChild(tmpTable);
			mc.name = "Table:" + ot.TableID ;			
			mc.x = xNew ;
			mc.y = yNew ;	
			tmpTableTemp.x = xNew ;
			tmpTableTemp.y = yNew ;
			movieTables.push(tmpTable) ;	
			movieTablesXY.push(tmpTableTemp) ;
			scrollPaneTable_mc.addChild(mc) ;
			scrTable.update() ;					
		}
		
		public static function DeleteTable(str : String) { //Delete mot Table
			var object = JSON.decode(str) ;	
			var strID : String = "Table:" + object.Id ;
			var _mc : MovieClip = MovieClip(scrollPaneTable_mc.getChildByName(strID)) ;			
			if (_mc != null)
				scrollPaneTable_mc.removeChild(_mc) ;			
			scrTable.update() ;
			for (var i:int = 0; i < movieTables.length; i++) 
			{				
				if (movieTables[i].name == strID) {
					movieTables.splice(i, 1) ;
					break ;
				}				
			}
			for (var j:int = 0; j < movieTablesXY.length; j++) 
			{				
				if (movieTablesXY[j].name == strID) {
					movieTablesXY.splice(j, 1) ;
					break ;
				}				
			}
		}
		
		public static function btnBack_mc_Click(evt:MouseEvent) { //Back tu Room ==> Level
			//{ Tao Tieng
			SoundKick() ;
			//}
			isSend = true ;
			movieRooms = null ;
			roomID = 0 ;
			isBegin = true ;
			//{ Tro ve man hinh Level
			mainStage_mc.gotoAndStop(3) ;
			//{Load BackGround
			if (mainStage_mc.BG_mc.numChildren > 1)
				mainStage_mc.BG_mc.removeChildAt(1) ;
			if (mainStage_mc.BGBanbe_mc.numChildren > 1)
				mainStage_mc.BGBanbe_mc.removeChildAt(1) ;
			if (mainStage_mc.BGCT_mc.numChildren > 1)
				mainStage_mc.BGCT_mc.removeChildAt(1) ;
			if (mainStage_mc.BGNew_mc.numChildren > 1)
				mainStage_mc.BGNew_mc.removeChildAt(1) ;
			if (mainStage_mc.BGLogo_mc.numChildren > 1)
				mainStage_mc.BGLogo_mc.removeChildAt(1) ;
			if (mainStage_mc.BGTop_mc.numChildren > 1)
				mainStage_mc.BGTop_mc.removeChildAt(1) ;
			if (ArrBG[0] != null)
				mainStage_mc.BG_mc.addChild(ArrBG[0]) ;
			if (ArrBG[1] != null)
				mainStage_mc.BGLogo_mc.addChild(ArrBG[1]) ;			
			if (ArrBG[2] != null) 
				mainStage_mc.BGBanbe_mc.addChild(ArrBG[2]) ;
			if (ArrBG[3] != null)
				mainStage_mc.BGCT_mc.addChild(ArrBG[3]) ;
			if (ArrBG[4] != null)
				mainStage_mc.BGNew_mc.addChild(ArrBG[4]) ;
			if (ArrBG[5] != null)
				mainStage_mc.BGTop_mc.addChild(ArrBG[5]) ;
			//}
			//{Hien thi thong tin nguoi choi
			if ((currPlayer.TinyURL != "") && (currPlayer.TinyURL != null)) {
				var imageLoader:Loader = new Loader();
				var image:URLRequest = new URLRequest(currPlayer.TinyURL) ;
				imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoader);				
				imageLoader.name = "BigAW" ;
				imageLoader.load(image);				
				function onLoader(evt:Event)
				{
					imageLoader.width = 50 ;
					imageLoader.height = 50 ;
					if (mainStage_mc.Avatar_mc.numChildren > 1)
						mainStage_mc.Avatar_mc.removeChildAt(1) ;
					mainStage_mc.Avatar_mc.addChild(imageLoader);
				}				
			}
			mainStage_mc.Name_txt.text = currPlayer.PlayerName ;
			mainStage_mc.Mark_txt.text = numberFormat(currPlayer.Mark, 0, false, true) + " AG";		
			NonPlayMoney = currPlayer.Mark;
			var formatText : TextFormat = new TextFormat() ;				
			formatText.font = "Tahoma" ;
			formatText.bold = true ;
			formatText.size = 16 ;
			mainStage_mc.Name_txt.setTextFormat(formatText) ;
			mainStage_mc.Mark_txt.setTextFormat(formatText) ;	
			// Khung qua tang
			//QuaTang_fromServer(str);
			mainStage_mc.Khung_Qua_mc.visible = false;
			//
			//}
			mainStage_mc.btnTransferAG.addEventListener(MouseEvent.CLICK, btnTransferAG_mc_Click) ;
			mainStage_mc.btnCTDG.addEventListener(MouseEvent.CLICK, btnCTDG_mc_Click) ;
			mainStage_mc.btnCT.addEventListener(MouseEvent.CLICK, btnCT_mc_Click) ;
			mainStage_mc.btnNew.addEventListener(MouseEvent.CLICK, btnNew_mc_Click) ;
			mainStage_mc.btnNapAG.addEventListener(MouseEvent.CLICK, btnNapAG_mc_Click) ;	
			//tf.appendText("\n truoc khi check date");
						//InitCTDG() ;
			var dtCheck : Date = new Date() ;
			if (dtCheck.day == 1 || dtCheck.date ==1)
			{
				//tf.appendText("\n Trong if vs date = 1");
					mainStage_mc.CTDG_mc.visible = false ;
				//mainStage_mc.CTDG_mc.DGTrongngay_mc.visible = false ;
					mainStage_mc.CT_mc.visible = false ;
					mainStage_mc.CT_mc.CTTuanThang_mc.visible = false ;
					mainStage_mc.CT_mc.CTThang_mc.visible = false ;
					mainStage_mc.CT_mc.CTTrongngay_mc.visible = false ;
					mainStage_mc.CT_mc.CTHomtruoc_mc.visible = false ;
					mainStage_mc.btnCT.gotoAndStop(1) ;
					mainStage_mc.CT_mc.CTTrongngay_btn.visible = false;
					mainStage_mc.CT_mc.CTHomtruoc_btn.visible = false;
					mainStage_mc.CT_mc.CTTuan_btn.visible = false;
					mainStage_mc.CT_mc.CTThang_btn.visible = false;
					mainStage_mc.btnCTDG.gotoAndStop(1) ;
					mainStage_mc.btnTransferAG.gotoAndStop(1) ;
					mainStage_mc.btnNew.gotoAndStop(2) ;
					mainStage_mc.KhungChuyenAG.visible = false ;
					mainStage_mc.New_mc.visible = true ;
					mainStage_mc.BGTop_mc.visible = false;
			} else {
				var Yesterday:Date = new Date();			
				Yesterday.setDate(Yesterday.getDate() - 1);
				var day1: String = Yesterday.getDate().toString();
				var month1 = (Yesterday.getMonth() + 1).toString()
				mainStage_mc.CT_mc.CTHomtruoc_mc.Title_txt.text = "Top Cao thủ Tá lả ngày " + day1 + "/" + month1;  
				
				mainStage_mc.CTDG_mc.visible = false ;
				//mainStage_mc.CTDG_mc.DGTrongngay_mc.visible = false ;
				mainStage_mc.CT_mc.visible = true ;
				mainStage_mc.CT_mc.CTTuanThang_mc.visible = false ;
				mainStage_mc.CT_mc.CTThang_mc.visible = false ;
				mainStage_mc.btnCT.gotoAndStop(2) ;
				mainStage_mc.CT_mc.CTTrongngay_btn.visible = false;
				mainStage_mc.CT_mc.CTHomtruoc_btn.visible = false;
				mainStage_mc.CT_mc.CTTuan_btn.visible = false;
				mainStage_mc.CT_mc.CTThang_btn.visible = false;
				mainStage_mc.btnCTDG.gotoAndStop(1) ;
				mainStage_mc.btnTransferAG.gotoAndStop(1) ;
				mainStage_mc.btnNew.gotoAndStop(1) ;
				mainStage_mc.KhungChuyenAG.visible = false ;
				mainStage_mc.New_mc.visible = false ;
				InitCTDG_CT();
			}
			for (var i:int = 0; i < arrLevelInfor.length; i++) 
			{
				var item:Object	 = arrLevelInfor[i];
				var oLR : ObjectLevelRoom = new ObjectLevelRoom() ;
				oLR.LevelID = item.Id ;
				oLR.LevelName = item.Name ;
				oLR.CurrPlayer = item.CurPlay ;
				oLR.MaxMark = item.MaxMark ;
				oLR.MinMark = item.MinMark ;				
				if (i == 0) {
					new LevelRoomController(mainStage_mc.level1_mc, oLR) ;
				} else if (i == 1) {
					new LevelRoomController(mainStage_mc.level2_mc, oLR) ;
				}
			}
			movieRooms = new Array() ;		
			//{ Kiem tra gui yeu cau Feed len tuong
			var markChange : Number = 0 ;
			var markS : Number ;
			//if (markInit != 0) {
				//markChange = currPlayer.Mark - markInit ;
				//if (currPlayer.Mark > 1000000) {					
					//markS = 0.3 * markInit ;
				//} else if (currPlayer.Mark > 100000) {
					//markS = 0.4 * markInit;
				//} else {
					//markS = 0.5 * markInit  ;
				//}
				//if (markChange > markS) {
					//ExternalInterface.call("pushfeed",user.UserId);
					//DisplayContactFriends("Mình quả là cao thủ :D")
				//} else {
					//if (markChange < 0 - markS)
						//ExternalInterface.call("pushfeed",user.UserId);
						//DisplayContactFriends("Hôm nay đen quá :((")
				//}
			//}
			LoadData() ;
			//tf.appendText(WinMoney.toString());
			tf.appendText("\n init money = " + InitMoney.toString());
			if (NonPlayMoney == InitMoney)
			{
				tf.appendText("ko play");
				ExternalInterface.call("pushfeed", user.UserId);
			}
			else if (PlayMoney - InitMoney > 0)
			{	
				tf.appendText("Play win");
				ExternalInterface.call("pushfeed", user.UserId, 3 );
			}
			else if(PlayMoney - InitMoney < 0)
			{
				tf.appendText("Play lose");
				ExternalInterface.call("pushfeed", user.UserId, 2 );
			}
			//markInit = currPlayer.Mark ;
			//}
			//}			
		}
		
		public static function clickJoinTable(_id:int):void	{ //Kick vao 1 Table de join vao choi
			//{ Tao Tieng
			SoundKick()
			//}
			isSend = true ;
			strTemp = "JT:" + _id + "ID:" + user.PlayId;
			mainSocket.sendData(strTemp);
			//Hide
			var tmpHide:MovieClip = MovieClip(new Hide());
			tmpHide.name = "Hide";
			tmpHide.x = 0 ;
			tmpHide.y = 0 ;
			tmpHide.alpha = 0 ;
			mainStage_mc.addChild(tmpHide) ;
			//Hien thi Man hinh Loading
			var tmpLD:MovieClip = MovieClip(new Loading());
			tmpLD.name = "Loading";
			tmpLD.x = 340;
			tmpLD.y = 250;			
			mainStage_mc.addChild(tmpLD);
		}
		
		public static function btnPlayGame(evt:MouseEvent) : void { //Kick nut choi ngay
			//{ Tao Tieng
			SoundKick()
			//}
			strTemp = "SearchTable:" + user.PlayId;
			mainSocket.sendData(strTemp);
		}
		
		public static function clickComeInTable(evt:MouseEvent) : void { //Kick nut Vao ban
			//{ Tao Tieng
			SoundKick()
			//}
			//Hide
			var tmpHide:MovieClip = MovieClip(new Hide());
			tmpHide.name = "Hide";
			tmpHide.x = 0 ;
			tmpHide.y = 0 ;
			tmpHide.alpha = 0 ;
			mainStage_mc.addChild(tmpHide) ;
			var tmpTable:MovieClip = MovieClip(new IDTable_Popup());
			if (ArrPopupBG[1] != null)
				tmpTable.BG_mc.addChild(ArrPopupBG[1]) ;
			tmpTable.name = "IDTable_Popup";
			tmpTable.x = 256;
			tmpTable.y = 100;
			tmpTable.addEventListener(KeyboardEvent.KEY_DOWN, keyComeInTable) ;
			tmpTable.btnXacnhan.addEventListener(MouseEvent.CLICK, btnXacnhan_ComeIn_click);
			tmpTable.btnHuybo.addEventListener(MouseEvent.CLICK, btnHuybo_ComeIn_click);			
			mainStage_mc.addChild(tmpTable);
		}
		public static function keyComeInTable(evt : KeyboardEvent) { //Xu ly Phim tren popup vao ban
			if (evt.keyCode == 13) {
				var tmp:MovieClip = MovieClip(mainStage_mc.getChildByName("IDTable_Popup")) ;
				var strID : String = tmp.txtID_mc.text ;
				if (strID == "") return ;
				strTemp = "JT:" + strID + "ID:" + user.PlayId;				
				mainSocket.sendData(strTemp);
				mainStage_mc.removeChild(tmp) ;
				var tmpHide:MovieClip = MovieClip(mainStage_mc.getChildByName("Hide"));
				while (tmpHide != null) {
					mainStage_mc.removeChild(tmpHide) ;
					tmpHide = MovieClip(mainStage_mc.getChildByName("Hide"));
				}
			}			
		}
		public static function btnXacnhan_ComeIn_click(evt:MouseEvent) { //Xu ly Kick Xac nhan tren popup vao ban
			//{ Tao Tieng
			SoundKick()
			//}
			var tmp:MovieClip = MovieClip(mainStage_mc.getChildByName("IDTable_Popup")) ;
			var strID : String = tmp.txtID_mc.text ;
			if (strID == "") return ;
			strTemp = "JT:" + strID + "ID:" + user.PlayId;
			mainSocket.sendData(strTemp);
			mainStage_mc.removeChild(tmp) ;
			var tmpHide:MovieClip = MovieClip(mainStage_mc.getChildByName("Hide"));			
			while (tmpHide != null) {
				mainStage_mc.removeChild(tmpHide) ;
				tmpHide = MovieClip(mainStage_mc.getChildByName("Hide"));
			}
		}		
		public static function btnHuybo_ComeIn_click(evt:MouseEvent) {//Xu ly Kick Huy bo tren popup vao ban
			//{ Tao Tieng
			SoundKick()
			//}
			var tmp :MovieClip = MovieClip(mainStage_mc.getChildByName("IDTable_Popup")) ;
			mainStage_mc.removeChild(tmp) ;
			var tmpHide:MovieClip = MovieClip(mainStage_mc.getChildByName("Hide"));			
			while (tmpHide != null) {
				mainStage_mc.removeChild(tmpHide) ;
				tmpHide = MovieClip(mainStage_mc.getChildByName("Hide"));
			}
		}
		
		public static function clickCreateTable(evt:MouseEvent):void	{ //Tao ban
			//{ Tao Tieng
			SoundKick()
			//}
			var tmpHide:MovieClip = MovieClip(new Hide());
			tmpHide.name = "HideCT";
			tmpHide.x = 0 ;
			tmpHide.y = 0 ;
			tmpHide.alpha = 0 ;
			mainStage_mc.addChild(tmpHide) ;
			var tmpTable:MovieClip = MovieClip(new Table_Popup());
			tmpTable.name = "Table_Popup";
			tmpTable.name_txt.text = "Kiếm tiền đê!" ;
			if (ArrPopupBG[0] != null)
				tmpTable.BG_mc.addChild(ArrPopupBG[0]) ;
			//Enable cac lua chon cho hop ly
			if (currLevel.MinMark == 0) {
				tmpTable.rb1.visible = true ;
				tmpTable.rb2.visible = true ;
				tmpTable.rb3.visible = true ;
				tmpTable.rb4.visible = true ;
				tmpTable.rb5.visible = false ;
				tmpTable.rb6.visible = false ;
				tmpTable.rb1.label = "20 AG" ;
				tmpTable.rb1.value = 20 ;
				tmpTable.rb1.enabled = true ;
				tmpTable.rb2.label = "50 AG" ;
				tmpTable.rb2.value = 50 ;
				tmpTable.rb2.enabled = true ;
				tmpTable.rb3.label = "100 AG" ;
				tmpTable.rb3.value = 100 ;
				tmpTable.rb3.enabled = true ;
				tmpTable.rb4.label = "200 AG" ;
				tmpTable.rb4.value = 200 ;
				tmpTable.rb4.enabled = true ;
				tmpTable.rb2.selected = true ;
			} else if (currLevel.MinMark == 5000) {
				tmpTable.rb1.visible = true ;
				tmpTable.rb2.visible = true ;
				tmpTable.rb3.visible = true ;
				tmpTable.rb4.visible = true ;
				tmpTable.rb5.visible = true ;
				tmpTable.rb6.visible = true ;
				tmpTable.rb1.label = "1,000 AG" ;
				tmpTable.rb1.value = 1000 ;
				tmpTable.rb1.enabled = true ;
				tmpTable.rb2.label = "2,000 AG" ;
				tmpTable.rb2.value = 2000 ;
				tmpTable.rb2.enabled = true ;
				tmpTable.rb3.label = "5,000 AG" ;
				tmpTable.rb3.value = 5000 ;
				tmpTable.rb3.enabled = true ;
				tmpTable.rb4.label = "10,000 AG" ;
				tmpTable.rb4.value = 10000 ;
				tmpTable.rb4.enabled = true ;
				tmpTable.rb5.label = "50,000 AG" ;
				tmpTable.rb5.value = 50000 ;
				tmpTable.rb5.enabled = true ;
				tmpTable.rb6.label = "100,000 AG" ;
				tmpTable.rb6.value = 100000 ;
				tmpTable.rb6.enabled = true ;
				tmpTable.rb3.selected = true ;		
			} 
			tmpTable.x = 256;
			tmpTable.y = 100;
			tmpTable.addEventListener(KeyboardEvent.KEY_DOWN, keyPopupTable) ;			
			tmpTable.btnOK_mc.addEventListener(MouseEvent.CLICK, btnOK_mc_click);			
			tmpTable.btnNotOK_mc.addEventListener(MouseEvent.CLICK, btnNotOK_mc_click);		
			mainStage_mc.addChild(tmpTable);			
		}
		public static function keyPopupTable(evt : KeyboardEvent) { //Xu ly Phim tren popup tao ban
			if (evt.keyCode == 13) {
				var tmp:MovieClip = MovieClip(mainStage_mc.getChildByName("Table_Popup")) ;
				var strName : String = tmp.name_txt.text ;			
				var rbg : Object = tmp.rb1.group ;
				if (rbg.selection == null) return ;
				if (strName.length > 100)
					strName = strName.substring(0, 100) ;
				strName = trim(strName) ;
				if (strName.length < 1) return ;				
				var strValue = rbg.selection.value ;
				strTemp = "CT:" + "Tbl:" + strName + "UM:" + strValue +"ID:" + user.PlayId;
				currTable.TableName = strName; 
				currTable.MarkUnit = rbg.selection.value;
				mainSocket.sendData(strTemp);
				mainStage_mc.removeChild(tmp) ;
				//Hien thi Man hinh Loading
				var tmpLD:MovieClip = MovieClip(new Loading());
				tmpLD.name = "LoadingCT";
				tmpLD.x = 340;
				tmpLD.y = 250;			
				mainStage_mc.addChild(tmpLD);
			}			
		}		
		public static function btnOK_mc_click(evt:MouseEvent) { //Xu ly Kick Xac nhan tren popup tao ban
			var tmp:MovieClip = MovieClip(mainStage_mc.getChildByName("Table_Popup")) ;
			var strName : String = tmp.name_txt.text ;			
			var rbg : Object = tmp.rb1.group ;
			if (rbg.selection == null) return ;
			if (strName.length > 100)
				strName = strName.substring(0, 100) ;
			strName = trim(strName) ;
			if (strName.length < 1) return ;
			var strValue = rbg.selection.value ;
			strTemp = "CT:" + "Tbl:" + strName + "UM:" + strValue +"ID:" + user.PlayId;
			currTable.TableName = strName; 
			currTable.MarkUnit = rbg.selection.value;
			mainSocket.sendData(strTemp);
			mainStage_mc.removeChild(tmp) ;
			//Hien thi Man hinh Loading
			var tmpLD:MovieClip = MovieClip(new Loading());
			tmpLD.name = "LoadingCT";
			tmpLD.x = 340;
			tmpLD.y = 250;			
			mainStage_mc.addChild(tmpLD);			
			//{ Tao Tieng
			SoundKick()
			//}
		}
		public static function btnNotOK_mc_click(evt:MouseEvent) { //Xu ly Kick Huy bo tren popup tao ban
			var tmp :MovieClip = MovieClip(mainStage_mc.getChildByName("Table_Popup")) ;
			mainStage_mc.removeChild(tmp) ;
			var tmpHide:MovieClip = MovieClip(mainStage_mc.getChildByName("HideCT"));			
			while (tmpHide != null) {
				mainStage_mc.removeChild(tmpHide) ;
				tmpHide = MovieClip(mainStage_mc.getChildByName("HideCT"));
			}
			//{ Tao Tieng
			SoundKick()
			//}
		}
		
		public static function btnKhoeBan_mc_Click(evt : MouseEvent) { //Chuyen AG
			ExternalInterface.call("pushfeed", user.UserId);
			//DisplayContactFriends(" Cả nhà vào cùng thử sức nào!") ;
		}
		
		public static function ResultTransferAG(str : String) { //Ham xu ly sau khi nhan thong bao chuyen tien thanh cong
			var tmpLD:MovieClip = MovieClip(mainStage_mc.getChildByName("Loading"));
			while (tmpLD != null) {
				mainStage_mc.removeChild(tmpLD) ;
				tmpLD = MovieClip(mainStage_mc.getChildByName("Loading"));
			}
			currPlayer.Mark = currPlayer.Mark - AGTransfer ;
			AGTransfer = 0 ;
			if (mainStage_mc.currentFrame == 4)
				mainStage_mc.Mark_txt.text = numberFormat(currPlayer.Mark, 0, false, true) + " AG";
			else if (mainStage_mc.currentFrame == 3) {
				var formatText : TextFormat = new TextFormat() ;				
				formatText.font = "Tahoma" ;
				formatText.bold = true ;
				formatText.size = 12 ;
				mainStage_mc.Mark_txt.text = numberFormat(currPlayer.Mark, 0, false, true) + " AG";
				mainStage_mc.Mark_txt.setTextFormat(formatText) ;
			}
			Add_Popup_Thongbao(str) ;
		}
		
		public static function clickVolumn2(myEvent:MouseEvent) { //Kick Tat va bat tieng
			//{ Tao Tieng
			SoundKick()
			//}
			if (isSound) {
				isSound = false ;
				mainStage_mc.btnVolumn2.gotoAndStop("inactive") ;
			} else {
				isSound = true ;
				mainStage_mc.btnVolumn2.gotoAndStop("active") ;
			}
		}
		public static function StartTimerConection(evt:TimerEvent) : void { //15s Gui 1 lech len Server de check Disconnect			
			if (TimerConection.currentCount % 14 == 1) {
				if ((user.PlayId == "") || (user.PlayId == null)) {
					mainSocket = new CustomSocket(host, port);
				} else 
					if (TimerConection.running)
						TimerConection.stop() ;
			}
			if (TimerConection.currentCount == TimerConection.repeatCount) {
				TimerConection.stop() ;
				//countConnect++ ;
				/*if (isSendTest) {
					var object : Object = new Object() ;
					object.ID = currTable.TableID ;
					object.IDName = currPlayer.PlayerID ;
					object.Data = "Cộng hòa xã hội chủ nghĩa Việt Nam:" ;
					mainSocket.sendData("Chat:" + JSON.encode(object)) ;
					//mainSocket.sendData("Cộng hòa xã hội chủ nghĩa Việt Nam:" + countConnect) ;				
				}*/
				//if (mainStage_mc.currentFrame == 5)
				//	mainSocket.sendData("RG:" + currTable.TableID + "ID:" + user.PlayId);
				//mainSocket.sendData("Cộng hòa xã hội chủ nghĩa Việt Nam:" + countConnect) ;
				/*if (mainStage_mc.currentFrame != 5)
					mainSocket.sendData("hahaha") ;
				else if (mainStage_mc.currentFrame == 5)
						if (currTable.Status != 1)
							mainSocket.sendData("hahaha") ;*/
				/*if (TimerConection.running)
					TimerConection.stop() ;
				TimerConection = new Timer(1000) ;
				TimerConection.repeatCount = 30 ;
				TimerConection.addEventListener(TimerEvent.TIMER, StartTimerConection);				
				TimerConection.start() ;*/
			}
		}
		/******************************************************************************************************/
		/******************************** Functions in Frame 5 ************************************************/
		/****************************************** Emotion ***************************************************/
		public static function StartEmotionBottom(ev : TimerEvent) : void {
			if (mainStage_mc.currentFrame != 5) return ;
			//tf.appendText("Count:" + emotionTimerBottom.currentCount) ;
			if (emotionTimerBottom.currentCount == emotionTimerBottom.repeatCount) {
				emotionTimerBottom.stop() ;
				//tf.appendText("==>Remove") ;
				var mc : MovieClip = MovieClip(mainStage_mc.getChildByName("Emotion0")) ;
				//tf.appendText("==>Remove1") ;
				if (mc != null)
					mainStage_mc.removeChild(mc) ;
				//else tf.appendText("==>null");
				//tf.appendText("==>OK") ;
			}
		}
		public static function StartEmotionRight(ev : TimerEvent) : void {			
			if (mainStage_mc.currentFrame != 5) return ;
			if (emotionTimerRight.currentCount == emotionTimerRight.repeatCount) {
				emotionTimerRight.stop() ;
				var mc : MovieClip = MovieClip(mainStage_mc.getChildByName("Emotion1")) ;
				if (mc != null)
					mainStage_mc.removeChild(mc) ;
			}
		}
		public static function StartEmotionTop(ev : TimerEvent) : void {
			if (mainStage_mc.currentFrame != 5) return ;
			if (emotionTimerTop.currentCount == emotionTimerTop.repeatCount) {
				emotionTimerTop.stop() ;
				var mc : MovieClip = MovieClip(mainStage_mc.getChildByName("Emotion2")) ;
				if (mc != null)
					mainStage_mc.removeChild(mc) ;
			}
		}
		public static function StartEmotionLeft(ev : TimerEvent) : void {
			if (mainStage_mc.currentFrame != 5) return ;
			if (emotionTimerLeft.currentCount == emotionTimerLeft.repeatCount) {
				emotionTimerLeft.stop() ;
				var mc : MovieClip = MovieClip(mainStage_mc.getChildByName("Emotion3")) ;
				if (mc != null)
					mainStage_mc.removeChild(mc) ;
			}
		}
		
		public static function Display_Emotion(type : String, xEmotion : Number, yEmotion : Number, name : String) {
			var mc : MovieClip ;
			if (type == "EA7") {				 
				mc = ArrEmotion[0] ;
			} else if (type == "EA8") {
				mc = ArrEmotion[1] ;
			} else if (type == "EA9") {
				mc = ArrEmotion[2] ;
			} else if (type == "EA10") {
				mc = ArrEmotion[3] ;
			} else if (type == "EA11") {
				mc = ArrEmotion[4] ;
			} else if (type == "EA12") {
				mc = ArrEmotion[5] ;
			} else if (type == "EA13") {
				mc = ArrEmotion[6] ;
			} else if (type == "EA14") {
				mc = ArrEmotion[7];
			} else if (type == "EA15") {
				mc = ArrEmotion[8] ;				
			}
			if (mc == null) return ;
			mc.x = xEmotion ;
			mc.y = yEmotion ;
			//tf.appendText("XY:" + xEmotion + "-" + yEmotion) ;
			mc.name = name ;
			mainStage_mc.addChild(mc) ;
		}
		
		public static function Display_Emotion_Control(str : String) : void {
			//tf.appendText("==>" + str) ;
			var mc : MovieClip = MovieClip(mainStage_mc.getChildByName("EmotionGroup")) ;
			while (mc != null) {
				mainStage_mc.removeChild(mc) ;
				mc = MovieClip(mainStage_mc.getChildByName("EmotionGroup")) ;
			}	
			var obj : Object = JSON.decode(str) ;
			var cs : int = 0 ;
			//{Xac dinh thang bi hien thi
			for (var i:int = 0; i < currTable.ArrPlayers.length; i++) 
			{
				if (currTable.ArrPlayers[i].PlayerName == obj.NName) {
					cs = i ;	
					break ;
				} /*else if (currTable.ArrPlayers[i].PlayerName == obj.Name) {
					var formatText : TextFormat = new TextFormat() ;				
					formatText.font = "Tahoma" ;
					formatText.bold = true ;
					formatText.size = 14 ;
					currTable.ArrPlayers[i].Mark = currTable.ArrPlayers[i].Mark - 20 ;
					if ((currPosition - i == -1) || (currPosition - i == 3)) {
						mainStage_mc.PlayerRight_mc.Mark_txt.text = numberFormat(currTable.ArrPlayers[i].Mark, 0, false, true);
						mainStage_mc.PlayerRight_mc.Mark_txt.setTextFormat(formatText) ;
					} else if ((currPosition - i == -2) || (currPosition - i == 2)) {
						mainStage_mc.PlayerTop_mc.Mark_txt.text = numberFormat(currTable.ArrPlayers[i].Mark, 0, false, true);
						mainStage_mc.PlayerTop_mc.Mark_txt.setTextFormat(formatText) ;
					} else if ((currPosition - i == -3) || (currPosition - i == 1)) {
						mainStage_mc.PlayerLeft_mc.Mark_txt.text = numberFormat(currTable.ArrPlayers[i].Mark, 0, false, true);
						mainStage_mc.PlayerLeft_mc.Mark_txt.setTextFormat(formatText) ;
					} else {
						mainStage_mc.PlayerBottom_mc.Mark_txt.text = numberFormat(currTable.ArrPlayers[i].Mark, 0, false, true);
						mainStage_mc.PlayerBottom_mc.Mark_txt.setTextFormat(formatText) ;
					}
				}*/
			}
			//}
			var mcRemove : MovieClip ;
			if ((currPosition - cs == -1) || (currPosition - cs == 3)) {
				xEmotion = xRightEmotionG ;
				yEmotion = yRightEmotionG ;
				mcRemove = MovieClip(mainStage_mc.getChildByName("Emotion1")) ;
				if (mcRemove != null)
					mainStage_mc.removeChild(mcRemove) ;
				if (emotionTimerRight.running) 
					emotionTimerRight.stop() ;					
				emotionTimerRight = new Timer(1000) ;
				emotionTimerRight.repeatCount = 5 ;
				emotionTimerRight.addEventListener(TimerEvent.TIMER, StartEmotionRight);				
				emotionTimerRight.start() ;
				Display_Emotion(obj.Type, xEmotion, yEmotion, "Emotion1") ;
			} else if ((currPosition - cs == -2) || (currPosition - cs == 2)) {
				xEmotion = xTopEmotionG ;
				yEmotion = yTopEmotionG ;
				mcRemove = MovieClip(mainStage_mc.getChildByName("Emotion2")) ;
				if (mcRemove != null)
					mainStage_mc.removeChild(mcRemove) ;
				if (emotionTimerTop.running) 
					emotionTimerTop.stop() ;
				emotionTimerTop = new Timer(1000) ;
				emotionTimerTop.repeatCount = 5 ;
				emotionTimerTop.addEventListener(TimerEvent.TIMER, StartEmotionTop);				
				emotionTimerTop.start() ;
				Display_Emotion(obj.Type, xEmotion, yEmotion, "Emotion2") ;
			} else if ((currPosition - cs == -3) || (currPosition - cs == 1)) {
				xEmotion = xLeftEmotionG ;
				yEmotion = yLeftEmotionG ;
				mcRemove = MovieClip(mainStage_mc.getChildByName("Emotion3")) ;
				if (mcRemove != null)
					mainStage_mc.removeChild(mcRemove) ;
				if (emotionTimerLeft.running) 
					emotionTimerLeft.stop() ;
				emotionTimerLeft = new Timer(1000) ;
				emotionTimerLeft.repeatCount = 5 ;
				emotionTimerLeft.addEventListener(TimerEvent.TIMER, StartEmotionLeft);
				emotionTimerLeft.start() ;
				Display_Emotion(obj.Type, xEmotion, yEmotion, "Emotion3") ;
			} else {
				xEmotion = xBottomEmotionG ;
				yEmotion = yBottomEmotionG ;
				mcRemove = MovieClip(mainStage_mc.getChildByName("Emotion0")) ;
				if (mcRemove != null)
					mainStage_mc.removeChild(mcRemove) ;
				if (emotionTimerBottom.running) 
					emotionTimerBottom.stop() ;
				emotionTimerBottom = new Timer(1000) ;
				emotionTimerBottom.repeatCount = 5 ;
				emotionTimerBottom.addEventListener(TimerEvent.TIMER, StartEmotionBottom);				
				emotionTimerBottom.start() ;
				Display_Emotion(obj.Type, xEmotion, yEmotion, "Emotion0") ;
			}
			if (obj.Name != obj.NName)
				mainStage_mc.txtAreaChat_mc.htmlText += "<font color=\"#000000\">====>" + obj.Name + " Gửi " + obj.NName + "</font>\n" ;
			xEmotion = 0 ;
			yEmotion = 0 ;
		}		
		
		public static function Emotion_Kick(type : String) : void {
			var object : Object = new Object() ;
			object.ID = currTable.TableID ;
			object.NName = nameEmotion ;
			object.Type = type ;
			mainSocket.sendData("EA:" + JSON.encode(object)) ;
		}
		public static function Emotion0_Kick(evt : MouseEvent) : void {
			Emotion_Kick("EA7") ;
		}
		public static function Emotion1_Kick(evt : MouseEvent) : void {
			Emotion_Kick("EA8") ;
		}
		public static function Emotion2_Kick(evt : MouseEvent) : void {
			Emotion_Kick("EA9") ;
		}
		public static function Emotion3_Kick(evt : MouseEvent) : void {
			Emotion_Kick("EA10") ;
		}
		public static function Emotion4_Kick(evt : MouseEvent) : void {
			Emotion_Kick("EA11") ;
		}
		public static function Emotion5_Kick(evt : MouseEvent) : void {
			Emotion_Kick("EA12") ;
		}
		public static function Emotion6_Kick(evt : MouseEvent) : void {
			Emotion_Kick("EA13") ;
		}
		public static function Emotion7_Kick(evt : MouseEvent) : void {
			Emotion_Kick("EA14") ;
		}
		public static function Emotion8_Kick(evt : MouseEvent) : void {
			Emotion_Kick("EA15") ;
		}
		
		public static function mouseEmotionMoveOut(evt : MouseEvent) : void {			
			if (mainStage_mc.currentFrame != 5) return ;
			if (!isDisplayEmotion) return ;
			var mc : MovieClip = MovieClip(mainStage_mc.getChildByName("EmotionGroup")) ;
			while (mc != null) {
				mainStage_mc.removeChild(mc) ;
				mc = MovieClip(mainStage_mc.getChildByName("EmotionGroup")) ;
			}
			isDisplayEmotion = false ;
		}
		public static function DisplayEmotionGroup(x : int, y: int) : void {
			var mc : MovieClip = MovieClip(new EmotionGroup()) ;
			mc.name = "EmotionGroup" ;
			mc.Emotion0_mc.addEventListener(MouseEvent.CLICK, Emotion0_Kick) ;
			mc.Emotion1_mc.addEventListener(MouseEvent.CLICK, Emotion1_Kick) ;
			mc.Emotion2_mc.addEventListener(MouseEvent.CLICK, Emotion2_Kick) ;
			mc.Emotion3_mc.addEventListener(MouseEvent.CLICK, Emotion3_Kick) ;
			mc.Emotion4_mc.addEventListener(MouseEvent.CLICK, Emotion4_Kick) ;
			mc.Emotion5_mc.addEventListener(MouseEvent.CLICK, Emotion5_Kick) ;
			mc.Emotion6_mc.addEventListener(MouseEvent.CLICK, Emotion6_Kick) ;
			mc.Emotion7_mc.addEventListener(MouseEvent.CLICK, Emotion7_Kick) ;
			mc.Emotion8_mc.addEventListener(MouseEvent.CLICK, Emotion8_Kick) ;
			mc.addEventListener(MouseEvent.ROLL_OUT, mouseEmotionMoveOut) ;
			isDisplayEmotion = true ;
			mc.x = x ;
			mc.y = y ;
			mainStage_mc.addChild(mc) ;
		}		
		public static function AvatarRight_Kick(evt : MouseEvent) : void {
			if (mainStage_mc.getChildByName("Emotion1") != null) return ;
			DisplayEmotionGroup(690, 255) ;			
			nameEmotion = mainStage_mc.PlayerRight_mc.Name_txt.text ;
		}
		public static function AvatarLeft_Kick(evt : MouseEvent) : void {
			if (mainStage_mc.getChildByName("Emotion3") != null) return ;
			DisplayEmotionGroup(20, 255) ;
			nameEmotion = mainStage_mc.PlayerLeft_mc.Name_txt.text ;
		}
		public static function AvatarTop_Kick(evt : MouseEvent) : void {
			if (mainStage_mc.getChildByName("Emotion2") != null) return ;
			DisplayEmotionGroup(180, 15) ;
			nameEmotion = mainStage_mc.PlayerTop_mc.Name_txt.text ;
		}	
		public static function AvatarBottom_Kick(evt : MouseEvent) : void {
			if (mainStage_mc.getChildByName("Emotion0") != null) return ;
			DisplayEmotionGroup(535, 450) ;
			nameEmotion = mainStage_mc.PlayerBottom_mc.Name_txt.text ;
		}
		/*************************************** Xu ly Avatar ****************************************************/
		/*********************************************************************************************************/
		public static function ResetXYValue() : void {
			//{ Khoi tao Toa do vi tri cac con bai
			positionCards = new Array() ;
			objPosition.x0 = cardX[0]; 
			objPosition.y0 = cardY[0];
			positionCards.push(objPosition);//0 Bai tren tay player 1
			objPosition = new Object();
			objPosition.x0 = cardX[1]; 
			objPosition.y0 = cardY[1]; 
			positionCards.push(objPosition);//1 Bai tren tay player 2
			objPosition = new Object();
			objPosition.x0 = cardX[2]; 
			objPosition.y0 = cardY[2];
			positionCards.push(objPosition);//2 Bai tren tay player 3
			objPosition = new Object();
			objPosition.x0 = cardX[3]; 
			objPosition.y0 = cardY[3];
			positionCards.push(objPosition);//3 Bai tren tay player 4
			//Vi tri cac con danh
			objPosition = new Object();
			objPosition.x0 = cardX[4]; 
			objPosition.y0 = cardY[4];
			positionCards.push(objPosition);//4 Bai danh cua Player 1
			objPosition = new Object();
			objPosition.x0 = cardX[5]; // 290; 
			objPosition.y0 = cardY[5];
			positionCards.push(objPosition);//5 Bai danh cua Player 2
			objPosition = new Object();
			objPosition.x0 = cardX[6]; 
			objPosition.y0 = cardY[6];
			positionCards.push(objPosition);//6 Bai danh cua Player 3
			objPosition = new Object();
			objPosition.x0 = cardX[7]; // 449; 
			objPosition.y0 = cardY[7];
			positionCards.push(objPosition);//7 Bai danh cua Player 4
			//Vi tri cac con ha
			objPosition = new Object();
			objPosition.x0 = cardX[8]; 
			objPosition.y0 = cardY[8];
			positionCards.push(objPosition);//8 Bai ha cua Player 1
			objPosition = new Object();
			objPosition.x0 = cardX[9]; 
			objPosition.y0 = cardY[9];
			positionCards.push(objPosition);//9 Bai ha cua Player 2
			objPosition = new Object();
			objPosition.x0 = cardX[10]; 
			objPosition.y0 = cardY[10];
			positionCards.push(objPosition);//10 Bai ha cua Player 3
			objPosition = new Object();
			objPosition.x0 = cardX[11]; 
			objPosition.y0 = cardY[11];
			positionCards.push(objPosition);//11 Bai ha cua Player 4
			//}
		}
		
		public static function LoadAvatar_Bottom(str : String) : void {
			if (str != null) 
				if (str != "") {
					if (mainStage_mc.PlayerBottom_mc.Avatar_mc.numChildren > 1) {
						mainStage_mc.PlayerBottom_mc.Avatar_mc.removeChildAt(1) ;
					}
					var imageLoader:Loader = new Loader();
					var image:URLRequest = new URLRequest(str) ;	
					imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoader);				
					imageLoader.name = "TinyAW" ;
					imageLoader.load(image);					
					function onLoader(evt:Event)
					{
						imageLoader.width = 46 ;
						imageLoader.height = 46 ;
						mainStage_mc.PlayerBottom_mc.Avatar_mc.addChild(imageLoader);						
					}					
				}
		}
		public static function LoadAvatar_Right(str : String) : void {
			if (str != null) 
				if (str != "") {
					if (mainStage_mc.PlayerRight_mc.Avatar_mc.numChildren > 1) {
						mainStage_mc.PlayerRight_mc.Avatar_mc.removeChildAt(1) ;
					}
					var imageLoader:Loader = new Loader();
					var image:URLRequest = new URLRequest(str) ;	
					imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoader);				
					imageLoader.name = "TinyAW" ;
					imageLoader.load(image);					
					function onLoader(evt:Event)
					{
						imageLoader.width = 46 ;
						imageLoader.height = 46 ;
						mainStage_mc.PlayerRight_mc.Avatar_mc.addChild(imageLoader);						
					}					
				}
		}
		public static function LoadAvatar_Top(str : String) : void {
			if (str != null) 
				if (str != "") {
					if (mainStage_mc.PlayerTop_mc.Avatar_mc.numChildren > 1) {
						mainStage_mc.PlayerTop_mc.Avatar_mc.removeChildAt(1) ;
					}
					var imageLoader:Loader = new Loader();
					var image:URLRequest = new URLRequest(str) ;	
					imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoader);				
					imageLoader.name = "TinyAW" ;
					imageLoader.load(image);					
					function onLoader(evt:Event)
					{
						imageLoader.width = 46 ;
						imageLoader.height = 46 ;
						mainStage_mc.PlayerTop_mc.Avatar_mc.addChild(imageLoader);						
					}					
				}
		}
		public static function LoadAvatar_Left(str : String) : void {
			if (str != null) 
				if (str != "") {
					if (mainStage_mc.PlayerLeft_mc.Avatar_mc.numChildren > 1) {
						mainStage_mc.PlayerLeft_mc.Avatar_mc.removeChildAt(1) ;
					}
					var imageLoader:Loader = new Loader();
					var image:URLRequest = new URLRequest(str) ;	
					imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoader);				
					imageLoader.name = "TinyAW" ;
					imageLoader.load(image);					
					function onLoader(evt:Event)
					{
						imageLoader.width = 46 ;
						imageLoader.height = 46 ;
						mainStage_mc.PlayerLeft_mc.Avatar_mc.addChild(imageLoader);						
					}					
				}
		}
		
		public static function LoadData_Bottom(name : String, mark : Number, ip : String, url : String, isReady : Boolean) : void {
			mainStage_mc.PlayerBottom_mc.visible = true ;
			mainStage_mc.PlayerBottom_mc.Name_txt.text = name;
			mainStage_mc.PlayerBottom_mc.IP_txt.text = ip;
			mainStage_mc.PlayerBottom_mc.Mark_txt.text = numberFormat(mark, 0, false, true) + " AG";
			mainStage_mc.PlayerBottom_mc.iconReady.visible = isReady ;
			mainStage_mc.PlayerBottom_mc.txtTime_mc.text = "" ;
			mainStage_mc.PlayerBottom_mc.btnKick_mc.visible = false ;
			mainStage_mc.PlayerBottom_mc.Avatar_mc.addEventListener(MouseEvent.CLICK, AvatarBottom_Kick) ;
			if (mark > vipMark)
				mainStage_mc.PlayerBottom_mc.Khung_mc.gotoAndStop(2) ;
			LoadAvatar_Bottom(url) ;
		}
		public static function LoadData_Right(name : String, mark : Number, ip : String, url : String, isReady : Boolean) : void {
			mainStage_mc.PlayerRight_mc.visible = true ;
			mainStage_mc.PlayerRight_mc.Name_txt.text = name;
			mainStage_mc.PlayerRight_mc.IP_txt.text = ip;
			mainStage_mc.PlayerRight_mc.Mark_txt.text = numberFormat(mark, 0, false, true) + " AG";
			mainStage_mc.PlayerRight_mc.iconReady.visible = isReady ;
			mainStage_mc.PlayerRight_mc.txtTime_mc.text = "" ;
			mainStage_mc.PlayerRight_mc.btnKick_mc.visible = false ;
			mainStage_mc.PlayerRight_mc.Avatar_mc.addEventListener(MouseEvent.CLICK, AvatarRight_Kick) ;			
			if (mark > vipMark)
				mainStage_mc.PlayerRight_mc.Khung_mc.gotoAndStop(2) ;
			LoadAvatar_Right(url) ;
		}
		public static function LoadData_Top(name : String, mark : Number, ip : String, url : String, isReady : Boolean) : void {
			mainStage_mc.PlayerTop_mc.visible = true ;
			mainStage_mc.PlayerTop_mc.Name_txt.text = name;
			mainStage_mc.PlayerTop_mc.IP_txt.text = ip;
			mainStage_mc.PlayerTop_mc.Mark_txt.text = numberFormat(mark, 0, false, true) + " AG";
			mainStage_mc.PlayerTop_mc.iconReady.visible = isReady ;
			mainStage_mc.PlayerTop_mc.txtTime_mc.text = "" ;
			mainStage_mc.PlayerTop_mc.btnKick_mc.visible = false ;
			mainStage_mc.PlayerTop_mc.Avatar_mc.addEventListener(MouseEvent.CLICK, AvatarTop_Kick) ;			
			if (mark > vipMark)
				mainStage_mc.PlayerTop_mc.Khung_mc.gotoAndStop(2) ;
			LoadAvatar_Top(url) ;
		}
		public static function LoadData_Left(name : String, mark : Number, ip : String, url : String, isReady : Boolean) : void {
			mainStage_mc.PlayerLeft_mc.visible = true ;
			mainStage_mc.PlayerLeft_mc.Name_txt.text = name;
			mainStage_mc.PlayerLeft_mc.IP_txt.text = ip;
			mainStage_mc.PlayerLeft_mc.Mark_txt.text = numberFormat(mark, 0, false, true) + " AG";
			mainStage_mc.PlayerLeft_mc.iconReady.visible = isReady ;
			mainStage_mc.PlayerLeft_mc.txtTime_mc.text = "" ;
			mainStage_mc.PlayerLeft_mc.btnKick_mc.visible = false ;
			mainStage_mc.PlayerLeft_mc.Avatar_mc.addEventListener(MouseEvent.CLICK, AvatarLeft_Kick) ;			
			if (mark > vipMark)
				mainStage_mc.PlayerLeft_mc.Khung_mc.gotoAndStop(2) ;
			LoadAvatar_Left(url) ;
		}
		
		public static function AddBtnReady() : void {
			if (mainStage_mc.currentFrame != 5) return ;
			var mcCheck : MovieClip = MovieClip(mainStage_mc.getChildByName("Sansang")) ;
			if (mcCheck != null) return ;
			var mcReady : MovieClip ;
			mcReady = MovieClip(new Sansang_ND()) ;
			mcReady.addEventListener(MouseEvent.CLICK, clickReadyTable) ;
			mcReady.name = "btnReady" ;
			mcReady.x = 345 ;
			mcReady.y = 237 ;			
			mainStage_mc.addChild(mcReady) ;
		}
		public static function AddBtnInitGame() : void {
			if (mainStage_mc.currentFrame != 5) return ;
			var mcCheck : MovieClip = MovieClip(mainStage_mc.getChildByName("Batdauchoi")) ;
			if (mcCheck != null) return ;
			var mcInitGame : MovieClip ;
			mcInitGame = MovieClip(new Batdau_ND()) ;
			mcInitGame.addEventListener(MouseEvent.CLICK, clickStartTable) ;
			mcInitGame.name = "btnInitGame" ;
			mcInitGame.x = 345 ;
			mcInitGame.y = 237 ;
			mainStage_mc.addChild(mcInitGame) ;
		}
		public static function RemoveBtnRead() : void {
			var tmp : MovieClip = MovieClip(mainStage_mc.getChildByName("btnReady")) ;
			while (tmp != null) {
				mainStage_mc.removeChild(tmp) ;
				tmp = MovieClip(mainStage_mc.getChildByName("btnReady")) ;
			}
		}
		public static function RemoveBtnInitGame() : void {
			var tmp : MovieClip = MovieClip(mainStage_mc.getChildByName("btnInitGame")) ;
			while (tmp != null) {
				mainStage_mc.removeChild(tmp) ;
				tmp = MovieClip(mainStage_mc.getChildByName("btnInitGame")) ;
			}
		}
		
		/*********************************************************************************************************/
		public static function StartTimerInTable(evt:TimerEvent) : void { //sau 2s hien thi nut thoat ban
			if (TimerInTable.currentCount == TimerInTable.repeatCount) {
				if (TimerInTable.running)
					TimerInTable.stop() ;
				if (mainStage_mc.currentFrame == 5) {
					if ((currTable.Status != 1) || (isView == true))
						mainStage_mc.btnBackTable.visible = true ;
				}
			}
		}
		public static function GetColor(cs : int) : String { //Lay mau cua cac IP trong ban choi
			for (var i:int = 0; i < currTable.ArrPlayers.length; i++) 
			{
				if (cs == i) continue ;
				if (currTable.ArrPlayers[cs].PlayerIP == currTable.ArrPlayers[i].PlayerIP) return "Do" ;
			}
			return "Trang" ;
		}
		public static function DisplayColorAgain() {
			var formatText : TextFormat = new TextFormat() ;				
			formatText.font = "Tahoma" ;
			formatText.bold = true ;
			formatText.size = 11 ;
			var formatTextName : TextFormat = new TextFormat() ;				
			formatTextName.font = "Tahoma" ;
			formatTextName.bold = true ;
			formatTextName.size = 13 ;
			formatTextName.color = 0xFFFF00 ;
			var formatTextMark : TextFormat = new TextFormat() ;				
			formatTextMark.font = "Tahoma" ;
			formatTextMark.bold = true ;
			formatTextMark.size = 11 ;
			formatTextMark.color = 0xFFFFFF ;
			for (var i:int = 0; i < currTable.ArrPlayers.length; i++) 
			{
				if (GetColor(i) == "Do")
					formatText.color = 0xFF0000 ;
				else 
					formatText.color = 0x996666 ;
				var positionKhung : int = 1 ;
				if (currTable.ArrPlayers[i].Mark > vipMark) {
					formatTextMark.color = 0x000000 ;
					positionKhung = 2 ;
				} else 
					formatTextMark.color = 0xFFFFFF ;
				if ((currPosition - i == -1) || (currPosition - i == 3)) {
					mainStage_mc.PlayerRight_mc.IP_txt.setTextFormat(formatText) ;
					mainStage_mc.PlayerRight_mc.Name_txt.setTextFormat(formatTextName) ;
					mainStage_mc.PlayerRight_mc.Mark_txt.setTextFormat(formatTextMark) ;
					mainStage_mc.PlayerRight_mc.Khung_mc.gotoAndStop(positionKhung) ;
				} else if ((currPosition - i== -2) || (currPosition - i == 2)) {
					mainStage_mc.PlayerTop_mc.IP_txt.setTextFormat(formatText) ;
					mainStage_mc.PlayerTop_mc.Name_txt.setTextFormat(formatTextName) ;
					mainStage_mc.PlayerTop_mc.Mark_txt.setTextFormat(formatTextMark) ;
					mainStage_mc.PlayerTop_mc.Khung_mc.gotoAndStop(positionKhung) ;
				} else if ((currPosition - i == -3) || (currPosition - i == 1)) {
					mainStage_mc.PlayerLeft_mc.IP_txt.setTextFormat(formatText) ;
					mainStage_mc.PlayerLeft_mc.Name_txt.setTextFormat(formatTextName) ;
					mainStage_mc.PlayerLeft_mc.Mark_txt.setTextFormat(formatTextMark) ;
					mainStage_mc.PlayerLeft_mc.Khung_mc.gotoAndStop(positionKhung) ;
				} else {
					mainStage_mc.PlayerBottom_mc.IP_txt.setTextFormat(formatText) ;
					mainStage_mc.PlayerBottom_mc.Name_txt.setTextFormat(formatTextName) ;
					mainStage_mc.PlayerBottom_mc.Mark_txt.setTextFormat(formatTextMark) ;
					mainStage_mc.PlayerBottom_mc.Khung_mc.gotoAndStop(positionKhung) ;
				}
			}
		}
				
		public static function ViewTable(str:String):void { //User vao view Table
			var tmpHide:MovieClip = MovieClip(mainStage_mc.getChildByName("Hide"));			
			while (tmpHide != null) {
				mainStage_mc.removeChild(tmpHide) ;
				tmpHide = MovieClip(mainStage_mc.getChildByName("Hide"));			
			}
			var tmpLD:MovieClip = MovieClip(mainStage_mc.getChildByName("Loading"));
			while (tmpLD != null) {
				mainStage_mc.removeChild(tmpLD) ;
				tmpLD = MovieClip(mainStage_mc.getChildByName("Loading"));
			}
			//tf.appendText("ViewExit Table::" + str + "\n") ;
			isView = true ;
			ResetXYValue() ;
			//{ Cap nhat thong tin Player cua ban
			var object = JSON.decode(str) ;
			currTable = new ObjectTable() ;
			currTable.TableID = object.Id ;
			currTable.TableName = object.Name ;
			currTable.MarkUnit = object.MarkUnit ;
			currPlayer.CurrTableID = object.Id ;
			currPlayer.IsCreateTable = false ;
			currPlayer.Type = 0 ; //Chi la nguoi view
			currTable.ArrPlayers = new Array() ;
			var arrP : Array = object.ArrPlay ;			
			var item ;
			for (var i:int = 0; i < arrP.length; i++) 
			{
				item = arrP[i];
				var objPl : ObjectPlayer = new ObjectPlayer() ;
				objPl.PlayerID = item.Name ;
				objPl.PlayerName = item.Name ;
				objPl.Mark = item.Mark ;
				objPl.Ready = item.isStart ;
				objPl.PlayerIP = item.sIP ;
				//objPl.BigURL = item.BigUrl ;
				objPl.TinyURL = item.TinyUrl ;
				objPl.ArrCard = new Array() ;
				objPl.ArrCardDow = new Array() ;
				objPl.ArrCardRemove = new Array() ;
				objPl.ArrMovie = new Array() ;
				objPl.ArrMovieDow = new Array() ;
				objPl.ArrMovieRemove = new Array() ;
				currTable.ArrPlayers.push(objPl) ;
			}
			currTable.CurrentPlayer = arrP.length ;
			currPosition = currTable.ArrPlayers.length - 1 ;	
			//}
			//tf.appendText("View img:\n") ;
			//{ Khoi tao giao dien ban choi
			mainStage_mc.gotoAndStop(5);
			if (mainStage_mc.BG_mc.numChildren > 1)
				mainStage_mc.BG_mc.removeChildAt(1) ;
			if (levelID == 0) {
				if (ArrBG[7] != null)
					mainStage_mc.BG_mc.addChild(ArrBG[7]) ;	
			} else {
				if (ArrBG[8] != null)
					mainStage_mc.BG_mc.addChild(ArrBG[8]) ;
			}
			AllowExit = true ;
			mainStage_mc.btnVolumn.addEventListener(MouseEvent.CLICK, clickVolumn) ;
			mainStage_mc.btnXepbai.addEventListener(MouseEvent.CLICK, clickSortCard) ;
			mainStage_mc.btnAddCard.addEventListener(MouseEvent.CLICK, clickAddCard) ;
			mainStage_mc.btnChat_mc.addEventListener(MouseEvent.CLICK, clickChat) ;			
			//mainStage_mc.btnReady.addEventListener(MouseEvent.CLICK, clickReadyTable) ;			
			//mainStage_mc.btnInitGame.addEventListener(MouseEvent.CLICK, clickStartTable) ;
			//AddBtnInitGame() ;
			//AddBtnReady() ;
			mainStage_mc.btnBackTable.addEventListener(MouseEvent.CLICK, clickExitTable) ;
			mainStage_mc.txtChat_mc.addEventListener(KeyboardEvent.KEY_DOWN, keyDowChat) ;
			//Khoi tao thoi gian hien thi nut Thoat ban			
			if (TimerInTable.running)
				TimerInTable.stop() ;
			TimerInTable = new Timer(1000) ;
			TimerInTable.repeatCount = 2 ;			
			TimerInTable.addEventListener(TimerEvent.TIMER, StartTimerInTable);			
			TimerInTable.start() ;
			if (isSound) {
				mainStage_mc.btnVolumn.gotoAndStop("active") ;
			} else {
				mainStage_mc.btnVolumn.gotoAndStop("inactive") ;
			}
			//Hien thi len giao dien Player
			mainStage_mc.MarkUnit_txt.text = currTable.MarkUnit + " -- Bàn:" + currTable.TableID;
			mainStage_mc.PlayerTop_mc.visible = false ;
			mainStage_mc.PlayerRight_mc.visible = false ;			
			mainStage_mc.PlayerLeft_mc.visible = false ;
			mainStage_mc.btnAddCard.visible = false ;
			//RemoveBtnInitGame()
			//mainStage_mc.btnInitGame.visible = false ;
			mainStage_mc.Card00.visible = false ;
			mainStage_mc.btnBackTable.visible = false ;
			mainStage_mc.btnHabai.gotoAndStop(2) ;			
			mainStage_mc.btnGuibai.gotoAndStop(2) ;
			mainStage_mc.btnDanhbai.gotoAndStop(2) ;
			mainStage_mc.btnDanhbai.removeEventListener(MouseEvent.CLICK, clickRemoveCard) ;
			mainStage_mc.btnXepbai.gotoAndStop(2) ;
			mainStage_mc.btnXepbai.removeEventListener(MouseEvent.CLICK, clickSortCard) ;
			//}
			//{ Thong tin Player 1
			LoadData_Bottom(currTable.ArrPlayers[currTable.ArrPlayers.length - 1].PlayerName, currTable.ArrPlayers[currTable.ArrPlayers.length - 1].Mark
							, currTable.ArrPlayers[currTable.ArrPlayers.length - 1].PlayerIP, currTable.ArrPlayers[currTable.ArrPlayers.length - 1].TinyURL, false) ;
			//}
			//Thong tin cac Player con lai
			if (currTable.ArrPlayers.length > 3) {
				LoadData_Right(currTable.ArrPlayers[0].PlayerName, currTable.ArrPlayers[0].Mark, currTable.ArrPlayers[0].PlayerIP, currTable.ArrPlayers[0].TinyURL, false) ;
				LoadData_Top(currTable.ArrPlayers[1].PlayerName, currTable.ArrPlayers[1].Mark, currTable.ArrPlayers[1].PlayerIP, currTable.ArrPlayers[1].TinyURL, false) ;
				LoadData_Left(currTable.ArrPlayers[2].PlayerName, currTable.ArrPlayers[2].Mark, currTable.ArrPlayers[2].PlayerIP, currTable.ArrPlayers[2].TinyURL, false) ;				
			} else if (currTable.ArrPlayers.length > 2) {
				LoadData_Top(currTable.ArrPlayers[0].PlayerName, currTable.ArrPlayers[0].Mark, currTable.ArrPlayers[0].PlayerIP, currTable.ArrPlayers[0].TinyURL, false) ;
				LoadData_Left(currTable.ArrPlayers[1].PlayerName, currTable.ArrPlayers[1].Mark, currTable.ArrPlayers[1].PlayerIP, currTable.ArrPlayers[1].TinyURL, false) ;				
			} else if (currTable.ArrPlayers.length > 1) {
				LoadData_Left(currTable.ArrPlayers[0].PlayerName, currTable.ArrPlayers[0].Mark, currTable.ArrPlayers[0].PlayerIP, currTable.ArrPlayers[0].TinyURL, false) ;
			}
			DisplayColorAgain() ;
			//Hien thi danh sach bai
			var arrOC : Array = object.ArrOCard ;
			if (arrOC.length >0) {
				mainStage_mc.Card00.visible = true ;
				mainStage_mc.Card00.vien_mc.visible = false;
				mainStage_mc.Card00.gotoAndStop(0);
				if (levelID == 0)
					mainStage_mc.Card00.gotoAndStop(54) ;
				else
					mainStage_mc.Card00.gotoAndStop(1) ;
			}
			
			for (var j:int = 0; j < arrOC.length; j++) 
			{
				item = arrOC[j];
				var cs : int = 0 ;
				var arrC : Array = item.ArrCard ;				
				for (var k:int = 0; k < currTable.ArrPlayers.length; k++) 
				{
					if (item.Name == currTable.ArrPlayers[k].PlayerName) {
						cs = k ;
						break ;
					}					
				}
				var objC : Card ;
				var _mc : MovieClip;
				if (item.Type == 1) { //Bai Danh
					//{Bai Danh
					for (var l:int = 0; l < arrC.length; l++) 
					{
						objC = new Card() ;
						objC.NumberCard = arrC[l].N ;
						objC.Suit = arrC[l].S ;						
						if (currPosition == cs) {
							objC.X = positionCards[4].x0 + l * 18 ;
							objC.Y = positionCards[4].y0 ;
						} else if ((currPosition - cs == -1) || (currPosition - cs == 3)) {
							objC.X = positionCards[5].x0 + l * 18 ;
							objC.Y = positionCards[5].y0 ;
						} else if ((currPosition - cs == 2) || (currPosition - cs == -2)) {
							objC.X = positionCards[6].x0 + l * 18 ;
							objC.Y = positionCards[6].y0 ;
						} else if ((currPosition - cs == 1) || (currPosition - cs == -3)) {
							objC.X = positionCards[7].x0 + l * 18 ;
							objC.Y = positionCards[7].y0 ;
						}  
						_mc = MovieClip(new card()) ;
						if ((objC.NumberCard != 0) && (objC.Suit != 0))
							_mc.gotoAndStop(objC.NumberCard + (objC.Suit - 1) * 13 + 1) ;
						else {
							if (levelID == 0 )
								_mc.gotoAndStop(54) ;
							else 
								_mc.gotoAndStop(1) ;
						}
						_mc.x = objC.X ;
						_mc.y = objC.Y ;
						_mc.vien_mc.visible = false ;
						mainStage_mc.addChild(_mc) ;
						currTable.ArrPlayers[cs].ArrCardRemove.push(objC) ;
						currTable.ArrPlayers[cs].ArrMovieRemove.push(_mc) ;
					} 
					//}
				} else if (item.Type == 0) { //Bai tren tay
					//{Bai tren tay
					for (var m:int = 0; m < arrC.length; m++) 
					{
						objC = new Card() ;
						objC.NumberCard = arrC[m].N ;
						objC.Suit = arrC[m].S ;
						if (currPosition == cs) {
							objC.X = positionCards[0].x0 + m * 18 ;
							objC.Y = positionCards[0].y0 ;
						} else if ((currPosition - cs == -1) || (currPosition - cs == 3)) {
							objC.X = positionCards[1].x0 ;
							objC.Y = positionCards[1].y0 + m * 18;
						} else if ((currPosition - cs == 2) || (currPosition - cs == -2)) {
							objC.X = positionCards[2].x0 + m * 18;
							objC.Y = positionCards[2].y0 ;
						} else if ((currPosition - cs == 1) || (currPosition - cs == -3)) {
							objC.X = positionCards[3].x0 ;
							objC.Y = positionCards[3].y0 + m * 18 ;
						}
						if (currPosition == cs)
							_mc = MovieClip(new card()) ;
						else 
							_mc = MovieClip(new card()) ;
						if ((objC.NumberCard != 0) && (objC.Suit != 0))
							_mc.gotoAndStop(objC.NumberCard + (objC.Suit - 1) * 13 + 1) ;
						else {
							if (levelID == 0 )
								_mc.gotoAndStop(54) ;
							else 
								_mc.gotoAndStop(1) ;
						}
						_mc.x = objC.X ;
						_mc.y = objC.Y ;
						_mc.vien_mc.visible = false ;
						mainStage_mc.addChild(_mc) ;
						currTable.ArrPlayers[cs].ArrCard.push(objC) ;
						currTable.ArrPlayers[cs].ArrMovie.push(_mc) ;
					}
					//}
				} else if (item.Type == 3) { //Bai Ha
					//{ Bai ha
					var arrCard : Array = new Array();
					var arrMovie : Array = new Array();
					for (var n:int = 0; n < arrC.length; n++) 
					{
						objC = new Card() ;
						objC.NumberCard = arrC[n].N ;
						objC.Suit = arrC[n].S ;
						if (currPosition == cs) {
							objC.X = positionCards[8].x0 + n * 18 ;
							objC.Y = positionCards[8].y0 + 28 * currTable.ArrPlayers[cs].ArrCardDow.length;
						} else if ((currPosition - cs == -1) || (currPosition - cs == 3)) {
							objC.X = positionCards[9].x0 + n * 18;
							objC.Y = positionCards[9].y0 + currTable.ArrPlayers[cs].ArrCardDow.length * 28 ;
						} else if ((currPosition - cs == 2) || (currPosition - cs == -2)) {
							objC.X = positionCards[10].x0 + n * 18;
							objC.Y = positionCards[10].y0 + currTable.ArrPlayers[cs].ArrCardDow.length * 28;
						} else if ((currPosition - cs == 1) || (currPosition - cs == -3)) {
							objC.X = positionCards[11].x0 + n * 18;
							objC.Y = positionCards[11].y0 + currTable.ArrPlayers[cs].ArrCardDow.length * 28 ;
						}
						_mc = MovieClip(new card()) ;
						if ((objC.NumberCard != 0) && (objC.Suit != 0))
							_mc.gotoAndStop(objC.NumberCard + (objC.Suit - 1) * 13 + 1) ;
						else {
							if (levelID == 0 )
								_mc.gotoAndStop(54) ;
							else 
								_mc.gotoAndStop(1) ;
						}
						_mc.x = objC.X ;
						_mc.y = objC.Y ;
						_mc.vien_mc.visible = false ;
						mainStage_mc.addChild(_mc) ;
						arrCard.push(objC) ;
						arrMovie.push(_mc) ;						
					}
					currTable.ArrPlayers[cs].ArrCardDow.push(arrCard) ;
					currTable.ArrPlayers[cs].ArrMovieDow.push(arrMovie) ;
					//}
				}
			}
		}		
		/*
		 * Ham xu ly khi Player join vao ban dang choi
		 * ==> Khoi tao cac thong tin cua ban
		 * ==> Neu so user trong ban < 4 Add thong tin user vao vi tri 0 ==> Nguoc lai add thong tin user vao vi tri 5
		 * ==> Add cac thong tin ban va hien thi
		 */
		public static function JoinTable(str : String) : void { //Thang Player join ban
			//mainStage_mc.bgMain_mc.visible = false;	
			var tmpHide:MovieClip = MovieClip(mainStage_mc.getChildByName("Hide"));			
			while (tmpHide != null) {
				mainStage_mc.removeChild(tmpHide) ;
				tmpHide = MovieClip(mainStage_mc.getChildByName("Hide"));			
			}
			var tmpLD:MovieClip = MovieClip(mainStage_mc.getChildByName("Loading"));			
			while (tmpLD != null) {
				mainStage_mc.removeChild(tmpLD) ;
				tmpLD = MovieClip(mainStage_mc.getChildByName("Loading"));
			}
			try {	
				ResetXYValue() ;
				//tf.appendText("view:" + isView) ;
				if (isView) {//Remove het cac quan bai cua van truoc, khoi tao lai van bai				
					if (currTable.ArrPlayers != null)
					for (var n:int = 0; n < currTable.ArrPlayers.length; n++) 
					{
						currTable.ArrPlayers[n].ArrCard = new Array() ;
						if (currTable.ArrPlayers[n].ArrMovie != null)
							while (currTable.ArrPlayers[n].ArrMovie.length != 0)
								mainStage_mc.removeChild(currTable.ArrPlayers[n].ArrMovie.pop()) ;
						currTable.ArrPlayers[n].ArrCardRemove = new Array() ;
						if (currTable.ArrPlayers[n].ArrMovieRemove != null)
							while (currTable.ArrPlayers[n].ArrMovieRemove.length != 0)
								mainStage_mc.removeChild(currTable.ArrPlayers[n].ArrMovieRemove.pop()) ;
						currTable.ArrPlayers[n].ArrCardDow = new Array() ;
						if (currTable.ArrPlayers[n].ArrMovieDow != null)
							while (currTable.ArrPlayers[n].ArrMovieDow.length != 0) {
								var arr : Array = currTable.ArrPlayers[n].ArrMovieDow.pop() ;
								if (arr != null)
									while (arr.length != 0)
										mainStage_mc.removeChild(arr.pop()) ;
							}
					}
					//Remove het bieu tuong
					var tmpNhat:MovieClip = MovieClip(mainStage_mc.getChildByName("Nhat"));
					if (tmpNhat != null)
						mainStage_mc.removeChild(tmpNhat) ;
					var tmpNhi:MovieClip = MovieClip(mainStage_mc.getChildByName("Nhi"));
					if (tmpNhi != null)
						mainStage_mc.removeChild(tmpNhi) ;
					var tmpBa:MovieClip = MovieClip(mainStage_mc.getChildByName("Ba"));
					if (tmpBa != null)
						mainStage_mc.removeChild(tmpBa) ;
					var tmpBet:MovieClip = MovieClip(mainStage_mc.getChildByName("Bet"));
					while (tmpBet != null) {
						mainStage_mc.removeChild(tmpBet) ;
						tmpBet = MovieClip(mainStage_mc.getChildByName("Bet"));
					}
					var tmpMom:MovieClip = MovieClip(mainStage_mc.getChildByName("Mom"));
					while (tmpMom != null) {
						mainStage_mc.removeChild(tmpMom) ;
						tmpMom = MovieClip(mainStage_mc.getChildByName("Mom"));
					}
					//AddBtnReady() ;
					//mainStage_mc.btnReady.visible = true ;	
					mainStage_mc.btnBackTable.visible = true ;
				} else {
					mainStage_mc.gotoAndStop(5);	
					if (mainStage_mc.BG_mc.numChildren > 1)
						mainStage_mc.BG_mc.removeChildAt(1) ;
					if (levelID == 0) {						
						if (ArrBG[7] != null)
							mainStage_mc.BG_mc.addChild(ArrBG[7]) ;	
					} else {
						if (ArrBG[8] != null)
							mainStage_mc.BG_mc.addChild(ArrBG[8]) ;	
					}			
					AllowExit = true ;
					mainStage_mc.btnVolumn.addEventListener(MouseEvent.CLICK, clickVolumn) ;
					mainStage_mc.btnXepbai.addEventListener(MouseEvent.CLICK, clickSortCard) ;
					mainStage_mc.btnAddCard.addEventListener(MouseEvent.CLICK, clickAddCard) ;			
					mainStage_mc.btnChat_mc.addEventListener(MouseEvent.CLICK, clickChat) ;	
					if (isSound) {
						mainStage_mc.btnVolumn.gotoAndStop("active") ;
					} else {
						mainStage_mc.btnVolumn.gotoAndStop("inactive") ;
					}	
					AddBtnReady() ;
					//mainStage_mc.btnReady.addEventListener(MouseEvent.CLICK, clickReadyTable) ;
					AddBtnInitGame() ;
					//mainStage_mc.btnInitGame.addEventListener(MouseEvent.CLICK, clickStartTable) ;
					mainStage_mc.btnBackTable.addEventListener(MouseEvent.CLICK, clickExitTable) ;
					mainStage_mc.txtChat_mc.addEventListener(KeyboardEvent.KEY_DOWN, keyDowChat) ;
					if (TimerInTable.running)
						TimerInTable.stop() ;
					TimerInTable = new Timer(1000) ;
					TimerInTable.repeatCount = 2 ;			
					TimerInTable.addEventListener(TimerEvent.TIMER, StartTimerInTable);			
					TimerInTable.start() ;
					mainStage_mc.btnBackTable.visible = false ;
				}
				//{ Cap nhat thong tin Player cua ban choi
				var object = JSON.decode(str) ;
				currTable = new ObjectTable() ;
				currTable.TableID = object.Id ;
				currTable.TableName = object.Name ;
				currTable.MarkUnit = object.MarkUnit ;
				currPlayer.CurrTableID = object.Id ;
				currPlayer.IsCreateTable = false ;
				currPlayer.Type = 1 ; //La nguoi choi
				currTable.ArrPlayers = new Array() ;
				var arrP : Array = object.ArrPlay ;			
				for (var i:int = 0; i < arrP.length; i++) 
				{
					var item = arrP[i];
					var objPl : ObjectPlayer = new ObjectPlayer() ;
					objPl.PlayerID = item.Name ;
					objPl.PlayerName = item.Name ;
					objPl.Mark = item.Mark ;
					objPl.Ready = item.isStart ;			
					objPl.Type = 1 ;
					objPl.PlayerIP = item.sIP ;
					//objPl.BigURL = item.BigUrl ;
					objPl.TinyURL = item.TinyUrl ;
					objPl.ArrCard = new Array() ;
					objPl.ArrCardDow = new Array() ;
					objPl.ArrCardRemove = new Array() ;
					objPl.ArrMovie = new Array() ;
					objPl.ArrMovieDow = new Array() ;
					objPl.ArrMovieRemove = new Array() ;
					currTable.ArrPlayers.push(objPl) ;
				}
				currTable.CurrentPlayer = arrP.length ;
				currPosition = currTable.ArrPlayers.length - 1 ;
				//}
				//tf.appendText("==>ABC") ;				
				//tf.appendText("==>DEF") ;
				isView = false ;
				//{ Hien thi len giao dien Ban choi				
				mainStage_mc.PlayerTop_mc.visible = false ;
				mainStage_mc.PlayerRight_mc.visible = false ;			
				mainStage_mc.PlayerLeft_mc.visible = false ;
				mainStage_mc.btnAddCard.visible = false ;
				RemoveBtnInitGame() ;
				//mainStage_mc.btnInitGame.visible = false ;
				mainStage_mc.Card00.visible = false ;				
				mainStage_mc.btnXepbai.gotoAndStop(1) ;
				mainStage_mc.btnHabai.gotoAndStop(2) ;
				mainStage_mc.btnGuibai.gotoAndStop(2) ;
				mainStage_mc.btnDanhbai.gotoAndStop(2) ;
				mainStage_mc.btnDanhbai.removeEventListener(MouseEvent.CLICK, clickRemoveCard) ;
				mainStage_mc.MarkUnit_txt.text = currTable.MarkUnit + " -- Bàn:" + currTable.TableID;
				//}
				//}
				//{ View thong tin cua minh
				LoadData_Bottom(currTable.ArrPlayers[currTable.ArrPlayers.length - 1].PlayerName, currTable.ArrPlayers[currTable.ArrPlayers.length - 1].Mark
								,currTable.ArrPlayers[currTable.ArrPlayers.length - 1].PlayerIP, currTable.ArrPlayers[currTable.ArrPlayers.length - 1].TinyURL, false) ;
				//View Thong tin cac nguoi choi khac
				if (currTable.ArrPlayers.length > 3) { //Neu co 4 nguoi choi
					LoadData_Right(currTable.ArrPlayers[0].PlayerName, currTable.ArrPlayers[0].Mark, currTable.ArrPlayers[0].PlayerIP, currTable.ArrPlayers[0].TinyURL, currTable.ArrPlayers[0].Ready) ;
					LoadData_Top(currTable.ArrPlayers[1].PlayerName, currTable.ArrPlayers[1].Mark, currTable.ArrPlayers[1].PlayerIP, currTable.ArrPlayers[1].TinyURL, currTable.ArrPlayers[1].Ready) ;
					LoadData_Left(currTable.ArrPlayers[2].PlayerName, currTable.ArrPlayers[2].Mark, currTable.ArrPlayers[2].PlayerIP, currTable.ArrPlayers[2].TinyURL, currTable.ArrPlayers[2].Ready) ;
				} else if (currTable.ArrPlayers.length > 2) { //Neu co 3 nguoi choi
					LoadData_Top(currTable.ArrPlayers[0].PlayerName, currTable.ArrPlayers[0].Mark, currTable.ArrPlayers[0].PlayerIP, currTable.ArrPlayers[0].TinyURL, currTable.ArrPlayers[0].Ready) ;
					LoadData_Left(currTable.ArrPlayers[1].PlayerName, currTable.ArrPlayers[1].Mark, currTable.ArrPlayers[1].PlayerIP, currTable.ArrPlayers[1].TinyURL, currTable.ArrPlayers[1].Ready) ;				
				} else if (currTable.ArrPlayers.length > 1) { //Neu co 2 nguoi choi
					LoadData_Left(currTable.ArrPlayers[0].PlayerName, currTable.ArrPlayers[0].Mark, currTable.ArrPlayers[0].PlayerIP, currTable.ArrPlayers[0].TinyURL, currTable.ArrPlayers[0].Ready) ;
				}
				DisplayColorAgain() ;
			} catch (ex : Error) {				
				//tf.appendText(ex.getStackTrace());
			}
		}
		/*
		 * Ham xu ly khi Player khac join ban cua minh dang choi
		 * ==> Them thong tin Player xem vao danh sach Player cua ban
		 * ==> Hien thi thong tin Player len giao dien neu van con 1 trong 4 cho
		 */ 
		public static function OtherJoinTable(str : String) : void { //Player khac join ban			
			if (currPlayer.IsCreateTable) {
				//mainStage_mc.btnInitGame.visible = false ;
				RemoveBtnInitGame() ;
			}
			var object = JSON.decode(str) ;
			var objPl : ObjectPlayer = new ObjectPlayer() ;
			objPl.PlayerID = object.Name ;
			objPl.PlayerName = object.Name ;
			objPl.Mark = object.Mark ;
			objPl.Type = 1 ;
			objPl.PlayerIP = object.sIP ;
			//objPl.BigURL = object.BigUrl ;
			objPl.TinyURL = object.TinyUrl ;
			objPl.ArrCard = new Array() ;
			objPl.ArrCardDow = new Array() ;
			objPl.ArrCardRemove = new Array() ;
			objPl.ArrMovie = new Array() ;
			objPl.ArrMovieDow = new Array() ;
			objPl.ArrMovieRemove = new Array() ;			
			var cs : int = -1;
			for (var i:int = 0; i < currTable.ArrPlayers.length; i++) 
			{
				if (currPlayer.PlayerName == currTable.ArrPlayers[i].PlayerName) {
					cs = i;
					break ;
				}				
			}	
			var formatText : TextFormat = new TextFormat() ;				
			formatText.font = "Tahoma" ;
			formatText.bold = true ;
			formatText.size = 14 ;			
			//{ Hien thi thong tin Player Join
			if (cs == currTable.ArrPlayers.length - 1) {
				LoadData_Right(objPl.PlayerName, objPl.Mark, objPl.PlayerIP, objPl.TinyURL, false) ;
				if (currPlayer.IsCreateTable) {
					mainStage_mc.PlayerRight_mc.btnKick_mc.visible = true ;
					mainStage_mc.PlayerRight_mc.btnKick_mc.addEventListener(MouseEvent.CLICK, KichRightPlayer) ;
				}
			} else if (cs == currTable.ArrPlayers.length-2) {
				LoadData_Top(objPl.PlayerName, objPl.Mark, objPl.PlayerIP, objPl.TinyURL, false) ;
				if (currPlayer.IsCreateTable) {
					mainStage_mc.PlayerTop_mc.btnKick_mc.visible = true ;
					mainStage_mc.PlayerTop_mc.btnKick_mc.addEventListener(MouseEvent.CLICK, KichTopPlayer) ;
				}
			} else {
				LoadData_Left(objPl.PlayerName, objPl.Mark, objPl.PlayerIP, objPl.TinyURL, false) ;				
				if (currPlayer.IsCreateTable) {
					mainStage_mc.PlayerLeft_mc.btnKick_mc.visible = true ;
					mainStage_mc.PlayerLeft_mc.btnKick_mc.addEventListener(MouseEvent.CLICK, KichLeftPlayer) ;
				}
			}
			//}
			currTable.ArrPlayers.push(objPl) ;
			currTable.CurrentPlayer++ ;
			DisplayColorAgain() ;			
		}
		
		public static function clickReadyTable(evt : MouseEvent):void { //Kick Ready san sang de choi
			//{ Tao Tieng
			SoundKick()
			//}
			strTemp = "RG:" + currTable.TableID + "ID:" + user.PlayId;
			mainSocket.sendData(strTemp);
			//mainStage_mc.btnReady.visible = false ;
			mainStage_mc.Card00.visible = false ;			
		}
		
		public static function ReadyTable(str : String) : void { //Xu ly sau khi Server gui thong tin Ready ve
			var object = JSON.decode(str) ;
			var playerName : String = object.Name ;
			if (object.Name == currPlayer.PlayerName)
				RemoveBtnRead() ;
				//mainStage_mc.btnReady.visible = false ;
			var isReady : Boolean = new Boolean(object.isStart) ;
			var cs : int = 0 ;
			for (var j:int = 0; j < currTable.ArrPlayers.length; j++) 
			{
				if (currTable.ArrPlayers[j].PlayerName == playerName) {
					cs = j ;
					currTable.ArrPlayers[j].Ready = true ;
					break ;
				}				
			}
			//tf.appendText("cs - currPosition:" + cs + "-" + currPosition + "-" + isReady) ;
			if ((currPosition - cs == 2) || (currPosition - cs == -2)) {
				mainStage_mc.PlayerTop_mc.iconReady.visible = true ;
			} else if ((currPosition - cs == 1) || (currPosition - cs == -3)) {
				mainStage_mc.PlayerLeft_mc.iconReady.visible = true ;
			} else if ((currPosition - cs == -1) || (currPosition - cs == 3)) {
				mainStage_mc.PlayerRight_mc.iconReady.visible = true ;
			} else if (currPosition - cs == 0) {
				mainStage_mc.PlayerBottom_mc.iconReady.visible = true ;
			}
			//Neu la Create Table thì hien thi nut Bat dau choi neu du ready
			if (currPlayer.IsCreateTable) {
				var totalReady : int = 0 ;
				for (var i:int = 0; i < currTable.ArrPlayers.length; i++) 
				{
					if (currTable.ArrPlayers[i].Ready) 
						totalReady++ ;				
				}
				//tf.appendText("Ready:" + totalReady + "\n") ;
				if ((totalReady == currTable.CurrentPlayer) && (totalReady > 1) && (currTable.Status != 1)) {
					//mainStage_mc.btnInitGame.visible = true ;
					AddBtnInitGame() ;
					currentAction = "Danh" ;			
				}
			}			
		}
		
		public static function StartTimeTake(evt:TimerEvent) : void { //Dem thoi gian an ==> Dong tien bay ==> Ket thuc
			if ((TakeTimer.currentCount == 3) && (mainStage_mc.currentFrame == 5)) {
				var _mcEdit : MovieClip = MovieClip(mainStage_mc.getChildByName("MarkDiscount")) ;
				if (_mcEdit != null)
					_mcEdit.MarkDiscount_txt.text = numberFormat(parseInt(_mcEdit.MarkDiscount_txt.text.replace(",","")) * 95 / 100, 0, false, true); 
			}
			if ((TakeTimer.currentCount == TakeTimer.repeatCount) && (mainStage_mc.currentFrame == 5)) {
				var _mc : MovieClip = MovieClip(mainStage_mc.getChildByName("MarkDiscount")) ;
				var formatText : TextFormat = new TextFormat() ;				
				formatText.font = "Tahoma" ;
				formatText.bold = true ;
				formatText.size = 11 ;
				var positionKhung : int = 1 ;
				if (currTable.ArrPlayers[TakeCS].Mark > vipMark) {
					formatText.color = 0x000000 ;
					positionKhung = 2 ;
				} else 
					formatText.color = 0xFFFFFF ;
				if (_mc != null)
					mainStage_mc.removeChild(_mc) ;
				if ((currPosition - TakeCS == -2) || (currPosition - TakeCS == 2)) {
					mainStage_mc.PlayerTop_mc.Mark_txt.text = numberFormat(currTable.ArrPlayers[TakeCS].Mark, 0, false, true) + " AG";
					mainStage_mc.PlayerTop_mc.Mark_txt.setTextFormat(formatText) ;
					mainStage_mc.PlayerTop_mc.Khung_mc.gotoAndStop(positionKhung) ;
				} else if ((currPosition - TakeCS == -1) || (currPosition - TakeCS == 3)) {
					mainStage_mc.PlayerRight_mc.Mark_txt.text = numberFormat(currTable.ArrPlayers[TakeCS].Mark, 0, false, true) + " AG";
					mainStage_mc.PlayerRight_mc.Mark_txt.setTextFormat(formatText) ;
					mainStage_mc.PlayerRight_mc.Khung_mc.gotoAndStop(positionKhung) ;
				} else if ((currPosition - TakeCS == 1) || (currPosition - TakeCS == -3)) {
					mainStage_mc.PlayerLeft_mc.Mark_txt.text = numberFormat(currTable.ArrPlayers[TakeCS].Mark, 0, false, true) + " AG";
					mainStage_mc.PlayerLeft_mc.Mark_txt.setTextFormat(formatText) ;
					mainStage_mc.PlayerLeft_mc.Khung_mc.gotoAndStop(positionKhung) ;
				} else {
					mainStage_mc.PlayerBottom_mc.Mark_txt.text = numberFormat(currTable.ArrPlayers[TakeCS].Mark, 0, false, true) + " AG";
					mainStage_mc.PlayerBottom_mc.Mark_txt.setTextFormat(formatText) ;
					mainStage_mc.PlayerBottom_mc.Khung_mc.gotoAndStop(positionKhung) ;
				}
				TakeTimer.stop() ;
			}
		}
		
		public static function ShowTime(value : int) { //Hien thi dong ho dem dung vi tri
			if (mainStage_mc.currentFrame == 5) {
				if ((currPosition - currentTimerRun == 2) || (currPosition - currentTimerRun == -2)) {
					mainStage_mc.PlayerTop_mc.txtTime_mc.text = value ;
				} else if ((currPosition - currentTimerRun == 1) || (currPosition - currentTimerRun == -3)) {
					mainStage_mc.PlayerLeft_mc.txtTime_mc.text = value ;
				} else if ((currPosition - currentTimerRun == -1) || (currPosition - currentTimerRun == 3)){
					mainStage_mc.PlayerRight_mc.txtTime_mc.text = value ;
				} else {
					mainStage_mc.PlayerBottom_mc.txtTime_mc.text = value ;
				}
			}
		}
		
		public static function StartTimeMain(evt:TimerEvent) : void { //Dem thoi gian cho moi thao tac		
			ShowTime(mainTimer.repeatCount - mainTimer.currentCount) ;
			if ((mainTimer.currentCount == mainTimer.repeatCount) && (mainStage_mc.currentFrame == 5)) {				
				if (currentTimerRun == currPosition) {
					if (currentAction == "Boc") {
						AddCard() ;
					} else if (currentAction == "Danh") {						
						var movieMove : MovieClip = MovieClip(mainStage_mc.getChildByName("MovieMove")) ;
						if (movieMove != null) {
							
							isTranfer = false ;
							currTable.ArrPlayers[currPosition].ArrMovie[csMove].visible = true ;
							mainStage_mc.removeChild(movieMove) ;
						}
						SortForNumber() ;						
						//{Tim kiem con thich hop de danh
						var cs : int = currTable.ArrPlayers[currPosition].ArrCard.length ;
						while (true) {
							cs-- ;
							var demN : int = 1 ;
							var demD : int = 1 ;
							var chay : int = cs ;
							if (currTable.ArrPlayers[currPosition].ArrCard[chay].State == 2) continue ;
							//Check tham gia vao phom ngang
							while ((chay < currTable.ArrPlayers[currPosition].ArrCard.length - 1) && (demN < 3)) {
								chay ++ ;
								if (currTable.ArrPlayers[currPosition].ArrCard[chay].NumberCard == currTable.ArrPlayers[currPosition].ArrCard[cs].NumberCard) 
									demN++ ;
								else chay = currTable.ArrPlayers[currPosition].ArrCard.length - 1 ;
							}
							chay = cs ;
							while ((chay > 0)  && (demN < 3)) {
								chay -- ;
								if (currTable.ArrPlayers[currPosition].ArrCard[chay].NumberCard == currTable.ArrPlayers[currPosition].ArrCard[cs].NumberCard) 
									demN++ ;
								else chay = 0 ;
							}							
							//Check tham gia vao phom doc
							chay = cs ;
							var kc : int = 1 ;
							while ((chay < currTable.ArrPlayers[currPosition].ArrCard.length - 1) && (demD < 3)) {
								chay ++ ;
								if ((currTable.ArrPlayers[currPosition].ArrCard[chay].Suit == currTable.ArrPlayers[currPosition].ArrCard[cs].Suit)
									&& (currTable.ArrPlayers[currPosition].ArrCard[chay].NumberCard == currTable.ArrPlayers[currPosition].ArrCard[cs].NumberCard + kc)){
									demD++ ;
									kc++ ;								
								}
							}
							chay = cs ;
							kc = 1 ;
							while ((chay > 0)  && (demD < 3)) {
								chay -- ;
								if ((currTable.ArrPlayers[currPosition].ArrCard[chay].Suit == currTable.ArrPlayers[currPosition].ArrCard[cs].Suit) 
									&& (currTable.ArrPlayers[currPosition].ArrCard[chay].NumberCard == currTable.ArrPlayers[currPosition].ArrCard[cs].NumberCard - kc)){
									demD++ ;
									kc++ ;
								}
							}
							//trace("Auto:" + cs + "-" + demD + "-" + demN) ;
							if ((demD < 3) && (demN < 3)){
								currTable.ArrPlayers[currPosition].ArrMovie[cs].y = currTable.ArrPlayers[currPosition].ArrMovie[cs].y - 20 ;	
								break ;
							}
						}	
						//}
						RemoveCard() ;
					} else if (currentAction == "AutoHa") {
						var movieMove : MovieClip = MovieClip(mainStage_mc.getChildByName("MovieMove")) ;
						if (movieMove != null) {							
							isTranfer = false ;
							currTable.ArrPlayers[currPosition].ArrMovie[csMove].visible = true ;
							mainStage_mc.removeChild(movieMove) ;
						}
						ChonHa_click() ;
					}
				}
			}			
		}
		
		/*Check co phải là phỏm dọc hay không
		Input: i,j,k - số thứ tự 3 quân bài đã được sắp xếp tăng dần
		Output: true - Có là phỏm*/
		public static function CheckContinuous(i:int, j:int, k:int) :Boolean {
			if (playerCards[i].Suit != playerCards[j].Suit) return false ;
			if (playerCards[i].Suit != playerCards[k].Suit) return false ;
			if (playerCards[i].NumberCard + 2 == playerCards[k].NumberCard 
			&& playerCards[i].NumberCard + 1 == playerCards[j].NumberCard) return true; //ijk
			if (playerCards[i].NumberCard + 2 == playerCards[j].NumberCard 
			&& playerCards[i].NumberCard + 1 == playerCards[k].NumberCard) return true; //ikj
			if (playerCards[j].NumberCard + 2 == playerCards[i].NumberCard 
			&& playerCards[j].NumberCard + 1 == playerCards[k].NumberCard) return true; //jki
			if (playerCards[j].NumberCard + 2 == playerCards[k].NumberCard 
			&& playerCards[j].NumberCard + 1 == playerCards[i].NumberCard) return true; //jik
            if (playerCards[k].NumberCard + 2 == playerCards[i].NumberCard 
			&& playerCards[k].NumberCard + 1 == playerCards[j].NumberCard) return true; //kji
			if (playerCards[k].NumberCard + 2 == playerCards[j].NumberCard 
			&& playerCards[k].NumberCard + 1 == playerCards[i].NumberCard) return true; //kij
            return false;            
		}
		public static function CheckContinuousDow(i:int, j:int, k:int) :Boolean {
			if (currTable.ArrPlayers[currPosition].ArrCard[i].Suit != currTable.ArrPlayers[currPosition].ArrCard[j].Suit) return false ;
			if (currTable.ArrPlayers[currPosition].ArrCard[i].Suit != currTable.ArrPlayers[currPosition].ArrCard[k].Suit) return false ;
			if (currTable.ArrPlayers[currPosition].ArrCard[i].NumberCard + 2 == currTable.ArrPlayers[currPosition].ArrCard[k].NumberCard 
			&& currTable.ArrPlayers[currPosition].ArrCard[i].NumberCard + 1 == currTable.ArrPlayers[currPosition].ArrCard[j].NumberCard) return true; //ijk
			return false;            
		}
		/*Check con bài thứ i có nằm trong phỏm ngang hay không
		Input: i-Số thứ tự quân bài
		Output: Số con bài có cùng number với nó*/
		public static function CheckCardNumber(cs:int, arr : Array) : int {
			var demN : int = 1 ;
			var chay : int = cs ;		
			var countCheck : int = 0 ;
			if (playerCards[cs].State == 2) countCheck++ ;
			//Check tham gia vao phom ngang
			while ((chay < playerCards.length - 1) && (demN < 4)) {
				chay ++ ;
				if (playerCards[chay].NumberCard != playerCards[cs].NumberCard) continue ;
				if (arr[chay] == "5") continue ;
				if (playerCards[chay].State == 2) countCheck++ ;
				if (countCheck < 2) demN++ ;				
			}
			chay = cs ;
			while ((chay > 0)  && (demN < 4)) {
				chay -- ;
				if (playerCards[chay].NumberCard != playerCards[cs].NumberCard) continue ;
				if (arr[chay] == "5") continue ;
				if (playerCards[chay].State == 2) countCheck++ ;
				if (countCheck < 2) demN++ ;				
			}
            return demN;            
        }
		public static function CheckCardNumberDow(cs:int, arr : Array) : int {
			var demN : int = 1 ;
			var chay : int = cs ;		
			var countCheck : int = 0 ;
			if (currTable.ArrPlayers[currPosition].ArrCard[cs].State == 2) countCheck++ ;
			//Check tham gia vao phom ngang
			while ((chay < currTable.ArrPlayers[currPosition].ArrCard.length - 1) && (demN < 4)) {
				chay ++ ;
				if (currTable.ArrPlayers[currPosition].ArrCard[chay].NumberCard != currTable.ArrPlayers[currPosition].ArrCard[cs].NumberCard) continue ;
				if (arr[chay] == "5") continue ;				
				if (currTable.ArrPlayers[currPosition].ArrCard[chay].State == 2) countCheck++ ;
				if (countCheck < 2) demN++ ;
			}
			chay = cs ;
			while ((chay > 0)  && (demN < 4)) {
				chay -- ;
				if (currTable.ArrPlayers[currPosition].ArrCard[chay].NumberCard != currTable.ArrPlayers[currPosition].ArrCard[cs].NumberCard) continue ;
				if (arr[chay] == "5") continue ;
				if (currTable.ArrPlayers[currPosition].ArrCard[chay].State == 2) countCheck++ ;
				if (countCheck < 2) demN++ ;				
			}
            return demN;            
        }
		/******************************** So con bai ket hop thanh phom doc***************************************************/
		public static function CountContinuous(cs : int) : int {
			var nhay : int = 1 ;			
			var countTake : int = 0 ;
			if (playerCards[cs].State == 2) countTake++ ;
			for (var i:int = cs+1; i < playerCards.length; i++) 
			{
				if (playerCards[i].State == 5) continue;
				if (playerCards[i].State == 2) countTake++;
				if (countTake > 1) return nhay ;
				if ((playerCards[i].NumberCard == playerCards[cs].NumberCard + nhay) 
					&& (playerCards[i].Suit == playerCards[cs].Suit)) {
						nhay++ ;
					}
			}
			return nhay ;
		}
		public static function CountContinuousDow(cs : int) : int {
			var nhay : int = 1 ;			
			var countTake : int = 0 ;
			if (currTable.ArrPlayers[currPosition].ArrCard[cs].State == 2) countTake++ ;
			for (var i:int = cs+1; i < currTable.ArrPlayers[currPosition].ArrCard.length; i++) 
			{
				if (currTable.ArrPlayers[currPosition].ArrCard[i].State == 5) continue;
				if (currTable.ArrPlayers[currPosition].ArrCard[i].State == 2) countTake++;
				if (countTake > 1) return nhay ;
				if ((currTable.ArrPlayers[currPosition].ArrCard[i].NumberCard == currTable.ArrPlayers[currPosition].ArrCard[cs].NumberCard + nhay) 
					&& (currTable.ArrPlayers[currPosition].ArrCard[i].Suit == currTable.ArrPlayers[currPosition].ArrCard[cs].Suit)) {
						nhay++ ;
					}
			}
			return nhay ;
		}
		/* Kiem tra xem con bai co nam trong phom doc khong
		 * Input: cs - chi so con bai
		 */
		public static function IsSuit(cs : int) : Boolean {
			for (var i:int = 0; i < playerCards.length; i++) 
			{
				if (i == cs) continue ;
				if (playerCards[i].Suit != playerCards[cs].Suit) continue ;
				if ((playerCards[i].NumberCard != playerCards[cs].NumberCard + 1) 
					&& (playerCards[i].NumberCard != playerCards[cs].NumberCard - 1)
					&& (playerCards[i].NumberCard != playerCards[cs].NumberCard - 2)
					&& (playerCards[i].NumberCard != playerCards[cs].NumberCard + 2)) continue ;					
				for (var j:int = i+1; j < playerCards.length; j++) 
				{
					if (j == cs) continue ; 
					if (playerCards[j].Suit != playerCards[cs].Suit) continue ;
					if (((playerCards[j].NumberCard == playerCards[cs].NumberCard + 1)
						&& (playerCards[j].NumberCard == playerCards[i].NumberCard - 1)) 
						|| ((playerCards[j].NumberCard == playerCards[i].NumberCard + 1)
						&& (playerCards[j].NumberCard == playerCards[cs].NumberCard - 1))) return true ;
					if (((playerCards[cs].NumberCard == playerCards[j].NumberCard + 1)
						&& (playerCards[cs].NumberCard == playerCards[i].NumberCard - 1))
						|| ((playerCards[cs].NumberCard == playerCards[i].NumberCard + 1)
						&& (playerCards[cs].NumberCard == playerCards[j].NumberCard - 1))) return true ;
					if (((playerCards[i].NumberCard == playerCards[cs].NumberCard + 1)
						&& (playerCards[i].NumberCard == playerCards[j].NumberCard - 1))
						|| ((playerCards[i].NumberCard == playerCards[j].NumberCard + 1)
						&& (playerCards[i].NumberCard == playerCards[cs].NumberCard - 1))) return true ;
				}				
			}
			return false ;
		}
		/* Kiem tra xem con bai co nam trong phom ngang khong
		 * Input: cs - chi so con bai
		 */
		public static function IsNumberCard(cs : int) : Boolean {
			var demN : int = 0 ;
			for (var i:int = 0; i < playerCards.length; i++)
			{
				if (playerCards[i].NumberCard == playerCards[cs].NumberCard) demN++ ;
				if (demN > 2) return true ;
			}
			return false ;
		}
		
		public static function AutoFinished() { //Tu dong Ha bai va ket thuc van
			SortForNumber() ;
			ArrState = new Array() ;
			playerCards = null ;
			playerCards = new Array() ;
			//Khoi tao cho mang ArrState 0 - 0 vao phom, 1 vao phom doc, 2 vao phom ngang - 3 vao ca phom doc va phom ngang
			for (var j:int = 0; j < currTable.ArrPlayers[currPosition].ArrCard.length; j++)
				playerCards.push(currTable.ArrPlayers[currPosition].ArrCard[j]) ;
			playerCards.sortOn(["NumberCard", "Suit"], [Array.NUMERIC, Array.NUMERIC]);
			//Khoi tao cho mang ArrState 0 - 0 vao phom, 1 vao phom doc, 2 vao phom ngang - 3 vao ca phom doc va phom ngang
			for (var i:int = 0; i < playerCards.length; i++) 
			{
				var isNgang : Boolean = IsNumberCard(i) ;
				var isDoc : Boolean = IsSuit(i) ;
				if (isNgang) {
					if (isDoc) 
						ArrState.push("3") ;						
					else 
						ArrState.push("2") ;					
				} else if (isDoc) 
					ArrState.push("1") ;					
				else 
					ArrState.push("0") ;					
			}
			CountSelection = 0 ;
			MinCard = 10 ;
			//Goi ham De Quy			
			Recursive(0) ;
			//tf.appendText("Result***:" + MinCard + "\n");
			//tf.appendText(ArrStateStore + "\n") ;
			if (MinCard < 10)
				AutoDownCard("HC") ;
		}
		
		public static function CheckMark() : void { //Check xem voi day lua chon thi ah con bao nhieu con bai tren tay
			CountSelection ++ ;
			//tf.appendText("Day:" + CountSelection + "-" + ArrState + "\n") ;
			var arrCheckState : Array = new Array() ;
			for (var l:int = 0; l < ArrState.length; l++) 
			{
				arrCheckState.push(ArrState[l]) ;				
			}			
			//Hạ hết các phỏm dọc
			var countTake : int = 0 ;
            for (var i:int = 0; i < playerCards.length - 2; i++)
            {
				if ((arrCheckState[i] != "1") && (arrCheckState[i] != "6")) continue;
                for (var j:int = i + 1; j < playerCards.length - 1; j++)
                {
					if ((arrCheckState[j] != "1") && (arrCheckState[j] != "6")) continue;
                    for (var k:int = j + 1; k < playerCards.length; k++)
                    {
						if ((arrCheckState[k] != "1") && (arrCheckState[k] != "6")) continue;
						//Dem so con bai an tren tay
						countTake = 0 ;
						if (playerCards[k].State == 2) countTake++ ;
						if (playerCards[i].State == 2) countTake++ ;
						if (playerCards[j].State == 2) countTake++ ;
						if (countTake > 1) continue ;
                        if (CheckContinuous(i, j, k))
                        {
							//tf.appendText("==>Vao phom doc:" + playerCards[i].Suit + playerCards[i].NumberCard) ;
							var nhay:int = 1;
							//Tim cac con bai co the noi thanh phom 4,5
							for (var h:int = k + 1; h < playerCards.length; h++)
							{
								if ((arrCheckState[h] != "1") && (arrCheckState[h] != "6")) continue;
								if (playerCards[k].Suit != playerCards[h].Suit) continue ;
								if (playerCards[h].NumberCard == playerCards[k].NumberCard + nhay) 
								{
									if (playerCards[h].State == 2) countTake++ ;
									if (countTake > 1) break ;
									//tf.appendText("==>Them Vao phom doc:" + playerCards[h].Suit + playerCards[h].NumberCard) ;
									//trace("==>Ngangh:" + currTable.ArrPlayers[currPosition].ArrCard[h].NumberCard + currTable.ArrPlayers[currPosition].ArrCard[h].Suit + "->" + nhay) ;
									if (CountContinuous(h)<3) //h không thuộc phỏm ngang nên đưa vào phỏm dọc luôn
									{ 
										nhay++;
										arrCheckState[h] = "5";											
									}                                            
								}
							}                               
							arrCheckState[i] = "5";
							arrCheckState[j] = "5";
							arrCheckState[k] = "5";
                        }
                    }
                }
            }
			//tf.appendText("==> Ha phom ngang Con lai\n") ;
            //Hạ các phỏm ngang
			for (var m:int = 0; m < arrCheckState.length; m++) 
			{
				if ((arrCheckState[m] != "2") && (arrCheckState[m] != "3")) continue;
                if (CheckCardNumber(m, arrCheckState) > 2)
                {
					//tf.appendText("==> Ha phom ngang:" + playerCards[m].NumberCard + playerCards[m].Suit + "\n") ;
					countTake = 0 ;
					if (playerCards[m].State == 2) countTake++ ;
					var chay : int = m;					
					while (chay < playerCards.length-1) {
						chay++ ;
						if (arrCheckState[chay] == "5") continue ;
						if (playerCards[m].NumberCard != playerCards[chay].NumberCard) continue ;
						if (playerCards[chay].State == 2) countTake++ ;
						if (countTake < 2) 
							arrCheckState[chay] = "5";						
					}
					chay = m ;
					while (chay > 0) {
						chay--
						if (arrCheckState[chay] == "5") continue ;
						if (playerCards[m].NumberCard != playerCards[chay].NumberCard) continue ;
						if (playerCards[chay].State == 2) countTake++ ;
						if (countTake < 2) 
							arrCheckState[chay] = "5";						
					}					
					arrCheckState[m] = "5";
                }				
			}			
            var returnValue : int = 0 ;
			var t : Boolean = true ;
			//tf.appendText("==> kiem tra so con bai con\n") ;
			//tf.appendText(arrCheckState + "\n");
            //Kiem tra so con bai khong con null
			for (var n:int = 0; n < arrCheckState.length; n++) 
			{
			    if (arrCheckState[n] != "5") {
					returnValue++;
					if (playerCards[n].State == 2) t = false ;
				}
            }
			//tf.appendText("==>" + t + MinCard + "****\n") ;
			if (t)
				if (MinCard > returnValue) {
					MinCard = returnValue ;
					ArrStateStore = new Array() ;
					for (var o:int = 0; o < ArrState.length; o++) 
					{
						ArrStateStore.push(ArrState[o]);					
					}
					//tf.appendText("KQ:" + returnValue + "-" + ArrStateStore + "\n") ;			
				}
            //tf.appendText("Result:" + returnValue + "\n");
		}
		
		public static function Recursive(i : int) : void {	//Ham de quy sinh day bai de ha phom		
			if (i == ArrState.length) {
				CheckMark() ;				
            } else if (MinCard > 1)
            {
				if (ArrState[i] == 3)
                {
					Recursive(i + 1);
					ArrState[i] = 6;
					Recursive(i + 1);
					if (ArrState[i] == 6)
					{
						ArrState[i] = 3;
						return;
					}
                }                
                Recursive(i + 1);
            }			
		}
		/******************************** Tu dong ha bai khi bai u ***************************************************/
		/*
		 * Input: str - Chuoi tien to "HC" || "HU"*/
		public static function AutoDownCard(strPre : String) { //Tu dong ha bai	
			if (strPre == "HU")
				if (mainTimer.running)
					mainTimer.stop() ;
			var strSend : String = "" ;
			var arr : Array = new Array() ;
			var arrMovie : Array = new Array() ;
			var countTake : int = 0 ;
			var _mc : MovieClip ;
			var strTotal : String = "" ;
			//Hạ hết các phỏm dọc
			for (var i:int = 0; i < currTable.ArrPlayers[currPosition].ArrCard.length - 2; i++)
            {
				if ((ArrStateStore[i] != "1") && (ArrStateStore[i] != "6")) continue;
                for (var j:int = i + 1; j < currTable.ArrPlayers[currPosition].ArrCard.length - 1; j++)
                {
					if ((ArrStateStore[j] != "1") && (ArrStateStore[j] != "6")) continue;
					for (var k:int = j + 1; k < currTable.ArrPlayers[currPosition].ArrCard.length; k++)
                    {
						if ((ArrStateStore[k] != "1") && (ArrStateStore[k] != "6")) continue;
						//Dem so con bai an tren tay
						countTake = 0 ;
						if (currTable.ArrPlayers[currPosition].ArrCard[k].State == 2) countTake++ ;
						if (currTable.ArrPlayers[currPosition].ArrCard[j].State == 2) countTake++ ;
						if (currTable.ArrPlayers[currPosition].ArrCard[i].State == 2) countTake++ ;						
						if (countTake > 1) continue ;						
                        if (CheckContinuousDow(i, j, k))
                        {
							//Neu cả 3 con đều không thuộc phỏm ngang							
							arr = new Array() ;
							arr.push(currTable.ArrPlayers[currPosition].ArrCard[i]) ;
							arr.push(currTable.ArrPlayers[currPosition].ArrCard[j]) ;
							arr.push(currTable.ArrPlayers[currPosition].ArrCard[k]) ;
							strSend = strPre + ":" + currTable.TableID + "CR:" ;
							strSend = strSend + currTable.ArrPlayers[currPosition].ArrCard[i].Suit + currTable.ArrPlayers[currPosition].ArrCard[i].NumberCard + "_" ;
							strSend = strSend + currTable.ArrPlayers[currPosition].ArrCard[j].Suit + currTable.ArrPlayers[currPosition].ArrCard[j].NumberCard + "_" ;
							strSend = strSend + currTable.ArrPlayers[currPosition].ArrCard[k].Suit + currTable.ArrPlayers[currPosition].ArrCard[k].NumberCard + "_" ;
							var nhay:int = 1;
							//Tim cac con bai co the noi thanh phom 4,5
							for (var h:int = k + 1; h < currTable.ArrPlayers[currPosition].ArrCard.length; h++)
							{
								if ((ArrStateStore[h] != "1") && (ArrStateStore[h] != "6")) continue;									
								if ((currTable.ArrPlayers[currPosition].ArrCard[h].NumberCard == currTable.ArrPlayers[currPosition].ArrCard[k].NumberCard + nhay) 
									&& (currTable.ArrPlayers[currPosition].ArrCard[k].Suit == currTable.ArrPlayers[currPosition].ArrCard[h].Suit))
								{
									if (currTable.ArrPlayers[currPosition].ArrCard[h].State == 2) countTake++ ;
									if (countTake > 1) break ;
									if (CountContinuousDow(h)<3) //h không thuộc phỏm ngang + Khong thuoc phom doc tren ==> Dua vao
									{ 
										nhay++;
										ArrStateStore[h] = "5";
										strSend = strSend + currTable.ArrPlayers[currPosition].ArrCard[h].Suit + currTable.ArrPlayers[currPosition].ArrCard[h].NumberCard + "_" ;								
										arr.push(currTable.ArrPlayers[currPosition].ArrCard[h]) ;
									}                                            
								}
							}                               
							ArrStateStore[i] = "5";
							ArrStateStore[j] = "5";
							ArrStateStore[k] = "5";
							/*arrMovie = new Array() ;
							for (var l:int = 0; l < arr.length; l++) 
							{
								_mc = MovieClip(new card()) ;
								_mc.x = arr[l].X ;
								_mc.y = arr[l].Y ;
								_mc.gotoAndStop(arr[l].NumberCard + (arr[l].Suit -1) * 13 + 1) ;
								moveCard(_mc, positionCards[8].x0 + l * 15, positionCards[8].y0 + currTable.ArrPlayers[currPosition].ArrCardDow.length * 23) ;
								mainStage_mc.addChild(_mc) ;
								arrMovie.push(_mc) ;
								arr[l].X = positionCards[8].x0 + l * 15;
								arr[l].Y = positionCards[8].y0 + currTable.ArrPlayers[currPosition].ArrCardDow.length * 23 ;				
							}
							//Hien thi giao dien duoi client
							currTable.ArrPlayers[currPosition].ArrCardDow.push(arr) ;
							currTable.ArrPlayers[currPosition].ArrMovieDow.push(arrMovie) ;*/
							//Gui thong tin len Server
							strSend = strSend.substring(0, strSend.length -1) ;
							strSend = strSend + "ID:" + user.PlayId ;
							strTotal = strTotal + strSend + "^";
						}
                    }
                }
            }
			//Hạ các phỏm ngang
			for (var m:int = 0; m < currTable.ArrPlayers[currPosition].ArrCard.length; m++)
            {
                if ((ArrStateStore[m] != "2") && (ArrStateStore[m] != "3")) continue;
				if (CheckCardNumberDow(m, ArrStateStore) > 2)
                {
					countTake = 0 ;
					if (currTable.ArrPlayers[currPosition].ArrCard[m].State == 2) countTake++ ;
					arr = new Array() ;
					strSend = strPre + ":" + currTable.TableID + "CR:" ;
					strSend = strSend + currTable.ArrPlayers[currPosition].ArrCard[m].Suit + currTable.ArrPlayers[currPosition].ArrCard[m].NumberCard + "_" ;
					ArrStateStore[m] = "5";
					arr.push(currTable.ArrPlayers[currPosition].ArrCard[m]) ;
					if (m + 1 < ArrStateStore.length)
						if (ArrStateStore[m + 1] != "5")
							if (currTable.ArrPlayers[currPosition].ArrCard[m].NumberCard == currTable.ArrPlayers[currPosition].ArrCard[m + 1].NumberCard) {
								if (currTable.ArrPlayers[currPosition].ArrCard[m+1].State == 2) countTake++ ;
								if (countTake <2) {
									ArrStateStore[m + 1] = "5";
									strSend = strSend + currTable.ArrPlayers[currPosition].ArrCard[m + 1].Suit + currTable.ArrPlayers[currPosition].ArrCard[m + 1].NumberCard + "_" ;
									arr.push(currTable.ArrPlayers[currPosition].ArrCard[m + 1]) ;
								}
							}
                    if (m + 2 < ArrStateStore.length)
						if (ArrStateStore[m + 2] != "5")
							if (currTable.ArrPlayers[currPosition].ArrCard[m].NumberCard == currTable.ArrPlayers[currPosition].ArrCard[m + 2].NumberCard) {
								if (currTable.ArrPlayers[currPosition].ArrCard[m+2].State == 2) countTake++ ;
								if (countTake < 2) {
									ArrStateStore[m + 2] = "5";
									strSend = strSend + currTable.ArrPlayers[currPosition].ArrCard[m + 2].Suit + currTable.ArrPlayers[currPosition].ArrCard[m + 2].NumberCard + "_" ;
									arr.push(currTable.ArrPlayers[currPosition].ArrCard[m + 2]) ;
								}
							}
                    if (m + 3 < ArrStateStore.length)
						if (ArrStateStore[m + 3] != "5")
							if (currTable.ArrPlayers[currPosition].ArrCard[m].NumberCard == currTable.ArrPlayers[currPosition].ArrCard[m + 3].NumberCard) {
								if (currTable.ArrPlayers[currPosition].ArrCard[m+3].State == 2) countTake++ ;
								if (countTake < 2) {
									ArrStateStore[m + 3] = "5";
									strSend = strSend + currTable.ArrPlayers[currPosition].ArrCard[m + 3].Suit + currTable.ArrPlayers[currPosition].ArrCard[m + 3].NumberCard + "_" ;
									arr.push(currTable.ArrPlayers[currPosition].ArrCard[m + 3]) ;
								}
							}
                    if (m - 1 > -1)
						if (ArrStateStore[m - 1] != "5")
							if (currTable.ArrPlayers[currPosition].ArrCard[m].NumberCard == currTable.ArrPlayers[currPosition].ArrCard[m - 1].NumberCard) {
								if (currTable.ArrPlayers[currPosition].ArrCard[m-1].State == 2) countTake++ ;
								if (countTake < 2) {
									ArrStateStore[m - 1] = "5";
									strSend = strSend + currTable.ArrPlayers[currPosition].ArrCard[m - 1].Suit + currTable.ArrPlayers[currPosition].ArrCard[m - 1].NumberCard + "_" ;
									arr.push(currTable.ArrPlayers[currPosition].ArrCard[m - 1]) ;
								}
							}
                    if (m - 2 > -1)
						if (ArrStateStore[m - 2] != "5")
							if (currTable.ArrPlayers[currPosition].ArrCard[m].NumberCard == currTable.ArrPlayers[currPosition].ArrCard[m - 2].NumberCard) {
								if (currTable.ArrPlayers[currPosition].ArrCard[m-2].State == 2) countTake++ ;
								if (countTake < 2) {
									ArrStateStore[m - 2] = "5";
									strSend = strSend + currTable.ArrPlayers[currPosition].ArrCard[m - 2].Suit + currTable.ArrPlayers[currPosition].ArrCard[m - 2].NumberCard + "_" ;
									arr.push(currTable.ArrPlayers[currPosition].ArrCard[m - 2]) ;
								}
							}
                    if (m - 3 > -1)
						if (ArrStateStore[m - 3] != "5")
							if (currTable.ArrPlayers[currPosition].ArrCard[m].NumberCard == currTable.ArrPlayers[currPosition].ArrCard[m - 3].NumberCard) {
								if (currTable.ArrPlayers[currPosition].ArrCard[m-3].State == 2) countTake++ ;
								if (countTake < 2) {
									ArrStateStore[m - 3] = "5";
									strSend = strSend + currTable.ArrPlayers[currPosition].ArrCard[m - 3].Suit + currTable.ArrPlayers[currPosition].ArrCard[m - 3].NumberCard + "_" ;
									arr.push(currTable.ArrPlayers[currPosition].ArrCard[m - 3]) ;
								}
							}
					/*arrMovie = new Array() ;
					for (var n:int = 0; n < arr.length; n++) 
					{
						_mc = MovieClip(new card()) ;
						_mc.x = arr[n].X ;
						_mc.y = arr[n].Y ;
						_mc.gotoAndStop(arr[n].NumberCard + (arr[n].Suit -1) * 13 + 1) ;
						moveCard(_mc, positionCards[8].x0 + n * 15, positionCards[8].y0 + currTable.ArrPlayers[currPosition].ArrCardDow.length * 23) ;
						mainStage_mc.addChild(_mc) ;
						arrMovie.push(_mc) ;
						arr[n].X = positionCards[8].x0 + n * 15;
						arr[n].Y = positionCards[8].y0 + currTable.ArrPlayers[currPosition].ArrCardDow.length * 23 ;						
					}					
                    currTable.ArrPlayers[currPosition].ArrCardDow.push(arr) ;
					currTable.ArrPlayers[currPosition].ArrMovieDow.push(arrMovie) ;*/
					strSend = strSend.substring(0, strSend.length -1) ;
					strSend = strSend + "ID:" + user.PlayId ;
					strTotal = strTotal + strSend + "^";
				}				
            }
			if (strTotal.search("00_00") == -1)
				mainSocket.sendData(strTotal) ;
			//Remove khoi danh sach bai va giao dien
			/*var t : Boolean = true ;
			while (t) {
				t = false ;
				for (var p:int = 0; p < currTable.ArrPlayers[currPosition].ArrCard.length; p++) 
				{
					//trace("AutoDow==>State:" + currTable.ArrPlayers[currPosition].ArrCard[n].State) ;
					if (ArrStateStore[p] == "5") {
						currTable.ArrPlayers[currPosition].ArrCard.splice(p, 1) ;
						ArrStateStore.splice(p, 1) ;
						mainStage_mc.removeChild(currTable.ArrPlayers[currPosition].ArrMovie[p]) ;
						currTable.ArrPlayers[currPosition].ArrMovie.splice(p, 1) ;
						t = true ;
						break ;
					}					
				}
				if (currTable.ArrPlayers[currPosition].ArrCard.length == 0) t = false ;
			}
			//Gan lai toa do cac con bai
			//Gan lai toa do cho danh sach con bai ==> Thay doi Movie tuong ung
			for (var o:int = 0; o < currTable.ArrPlayers[currPosition].ArrMovie.length; o++) 
			{
				currTable.ArrPlayers[currPosition].ArrCard[o].X = 325 + o * 20 ;
				currTable.ArrPlayers[currPosition].ArrCard[o].Y = 426 ;		
				currTable.ArrPlayers[currPosition].ArrMovie[o].x = 325 + o * 20 ;
				currTable.ArrPlayers[currPosition].ArrMovie[o].y = 426 ;
				var position :Number = currTable.ArrPlayers[currPosition].ArrCard[o].NumberCard + (currTable.ArrPlayers[currPosition].ArrCard[o].Suit - 1) * 13 + 1 ;
				currTable.ArrPlayers[currPosition].ArrMovie[o].gotoAndStop(position) ;	
				if (currTable.ArrPlayers[currPosition].ArrCard[o].State == 2)
					currTable.ArrPlayers[currPosition].ArrMovie[o].vien_mc.visible = true ;
				else
					currTable.ArrPlayers[currPosition].ArrMovie[o].vien_mc.visible = false ;				
			}*/
		}
		
		public static function InitTimeMain() {
			if (mainTimer.running)
				mainTimer.stop() ;
			mainTimer = new Timer(1000) ;
			mainTimer.repeatCount = 20 ;
			mainTimer.addEventListener(TimerEvent.TIMER, StartTimeMain);				
			mainTimer.start() ;
		}
		
		public static function clickStartTable(evt : MouseEvent):void {				
			//{ Tao Tieng
			SoundKick()
			//}
			//FinishedGame() ;
			strTemp = "SG:" + currTable.TableID + "ID:" + user.PlayId;
			mainSocket.sendData(strTemp);
			/*objPosition.x0 = 325; 
			objPosition.y0 = 426;
			positionCards.push(objPosition);//0 Bai tren tay player 1
			objPosition = new Object();
			objPosition.x0 = 650; 
			objPosition.y0 = 142; 
			positionCards.push(objPosition);//1 Bai tren tay player 2
			objPosition = new Object();
			objPosition.x0 = 325; 
			objPosition.y0 = 28;
			positionCards.push(objPosition);//2 Bai tren tay player 3
			objPosition = new Object();
			objPosition.x0 = 82; 
			objPosition.y0 = 142;
			positionCards.push(objPosition);//3 Bai tren tay player 4
			//Vi tri cac con danh
			objPosition = new Object();
			objPosition.x0 = 349; 
			objPosition.y0 = 318;
			positionCards.push(objPosition);//4 Bai danh cua Player 1
			objPosition = new Object();
			objPosition.x0 = 581; // 290; 
			objPosition.y0 = 227;
			positionCards.push(objPosition);//5 Bai danh cua Player 2
			objPosition = new Object();
			objPosition.x0 = 349; 
			objPosition.y0 = 117;
			positionCards.push(objPosition);//6 Bai danh cua Player 3
			objPosition = new Object();
			objPosition.x0 = 153; // 449; 
			objPosition.y0 = 227;
			positionCards.push(objPosition);//7 Bai danh cua Player 4
			//Vi tri cac con ha
			objPosition = new Object();
			objPosition.x0 = 483; 
			objPosition.y0 = 318;
			positionCards.push(objPosition);//8 Bai ha cua Player 1
			objPosition = new Object();
			objPosition.x0 = 483; 
			objPosition.y0 = 117;
			positionCards.push(objPosition);//9 Bai ha cua Player 2
			objPosition = new Object();
			objPosition.x0 = 170; 
			objPosition.y0 = 117;
			positionCards.push(objPosition);//10 Bai ha cua Player 3
			objPosition = new Object();
			objPosition.x0 = 170; 
			objPosition.y0 = 318;
			positionCards.push(objPosition);//11 Bai ha cua Player 4
			
			currPosition = 0 ;
			if (currTable.ArrPlayers[0].ArrCard == null) currTable.ArrPlayers[0].ArrCard = new Array() ;
			var obj : Card = new Card() ;
			obj.NumberCard = 1 ;
			obj.Suit = 1 ;
			obj.State = 0 ;
			currTable.ArrPlayers[0].ArrCard.push(obj) ;
			obj = new Card() ;
			obj.NumberCard = 1 ;
			obj.Suit = 2 ;
			obj.State = 0 ;
			currTable.ArrPlayers[0].ArrCard.push(obj) ;
			obj = new Card() ;
			obj.NumberCard = 1 ;
			obj.Suit = 4 ;
			obj.State = 2 ;
			currTable.ArrPlayers[0].ArrCard.push(obj) ;
			obj = new Card() ;
			obj.NumberCard = 7 ;
			obj.Suit = 4 ;
			obj.State = 0 ;
			currTable.ArrPlayers[0].ArrCard.push(obj) ;
			obj = new Card() ;
			obj.NumberCard = 8 ;
			obj.Suit = 4 ;
			obj.State = 0 ;
			currTable.ArrPlayers[0].ArrCard.push(obj) ;
			obj = new Card() ;
			obj.NumberCard = 9 ;
			obj.Suit = 4 ;
			obj.State = 4 ;
			currTable.ArrPlayers[0].ArrCard.push(obj) ;
			obj = new Card() ;
			obj.NumberCard = 10 ;
			obj.Suit = 4 ;
			obj.State = 0 ;
			currTable.ArrPlayers[0].ArrCard.push(obj) ;
			obj = new Card() ;
			obj.NumberCard = 11 ;
			obj.Suit = 4 ;
			obj.State = 0 ;
			currTable.ArrPlayers[0].ArrCard.push(obj) ;
			obj = new Card() ;
			obj.NumberCard = 12 ;
			obj.Suit = 4 ;
			obj.State = 2 ;
			currTable.ArrPlayers[0].ArrCard.push(obj) ;
			obj = new Card() ;
			obj.NumberCard = 2;
			obj.Suit = 2 ;
			obj.State = 0;
			currTable.ArrPlayers[0].ArrCard.push(obj) ;
			//tf.appendText("Start:" + currTable.ArrPlayers[0].ArrCard.length) ;
			if (currTable.ArrPlayers[0].ArrMovie == null) currTable.ArrPlayers[0].ArrMovie = new Array() ;
			for (var i:int = 0; i < currTable.ArrPlayers[0].ArrCard.length; i++) 
			{
				tf.appendText("==>" + currTable.ArrPlayers[0].ArrCard[i].NumberCard + "-" + currTable.ArrPlayers[0].ArrCard[i].Suit) ;
				var _mc : MovieClip = MovieClip(new card()) ;
				_mc.x = positionCards[0].x0 + i * 20 ;
				_mc.y = positionCards[0].y0 ;
				_mc.gotoAndStop(currTable.ArrPlayers[0].ArrCard[i].NumberCard + (currTable.ArrPlayers[0].ArrCard[i].Suit - 1) * 13 + 1) ;
				mainStage_mc.addChild(_mc) ;
				currTable.ArrPlayers[0].ArrMovie.push(_mc) ;
			}
			//tf.appendText("Start Dow") ;
			//AutoDownCard("HU") ;
			AutoFinished() ;*/
		}
		public static function Clean_Frame5() {			
			for (var i:int = 0; i < currTable.ArrPlayers.length; i++) 
			{
				//tf.appendText("Cur:" + currPosition + "-" + i) ;
				if ((currPosition - i == -1) || (currPosition - i == 3)) {	
					mainStage_mc.PlayerRight_mc.iconReady.visible = false ;
					mainStage_mc.PlayerRight_mc.txtTime_mc.text = "" ;
					if (currPlayer.IsCreateTable) 
						mainStage_mc.PlayerRight_mc.btnKick_mc.visible = false ;					
				} else if ((currPosition - i == -2) || (currPosition - i == 2)) {
					mainStage_mc.PlayerTop_mc.iconReady.visible = false ;
					mainStage_mc.PlayerTop_mc.txtTime_mc.text = "" ;
					if (currPlayer.IsCreateTable) 
						mainStage_mc.PlayerTop_mc.btnKick_mc.visible = false ;	
				} else if ((currPosition - i == -3) || (currPosition - i == 1)) {
					mainStage_mc.PlayerLeft_mc.iconReady.visible = false ;
					mainStage_mc.PlayerLeft_mc.txtTime_mc.text = "" ;
					//tf.appendText("OK:") ;
					if (currPlayer.IsCreateTable) {
						//tf.appendText("1") ;
						mainStage_mc.PlayerLeft_mc.btnKick_mc.visible = false ;	
						//tf.appendText("2") ;
					}
				} 
			}
			mainStage_mc.PlayerBottom_mc.iconReady.visible = false ;
			mainStage_mc.PlayerBottom_mc.txtTime_mc.text = "" ;
		}
		
		public static function InitGame(strLC : String) : void {
			UName = "";
			ChotName = "";
			tf.appendText("LC:" + strLC + "\n") ;		
			//An nut thoat ban
			AllowExit = false ;
			ArrExitTable = new Array() ;
			ArrUpdateMoney = new Array() ;
			if (currPlayer.Type !=0)
				mainStage_mc.btnBackTable.visible = false ;
			Clean_Frame5() ;
			mainStage_mc.btnXepbai.gotoAndStop(1) ;
			mainStage_mc.btnXepbai.addEventListener(MouseEvent.CLICK, clickSortCard) ;
			mainStage_mc.btnHabai.gotoAndStop(2) ;
			mainStage_mc.btnHabai.removeEventListener(MouseEvent.CLICK, clickDownCard) ;
			mainStage_mc.btnGuibai.gotoAndStop(2) ;
			mainStage_mc.btnGuibai.removeEventListener(MouseEvent.CLICK, clickAttachCard) ;
			mainStage_mc.btnDanhbai.gotoAndStop(2) ;
			mainStage_mc.btnDanhbai.removeEventListener(MouseEvent.CLICK, clickRemoveCard) ;				
			var i:Number;
			var j:Number;
			//{Remove het bieu tuong
			var tmpNhat:MovieClip = MovieClip(mainStage_mc.getChildByName("Nhat"));
			if (tmpNhat != null)
				mainStage_mc.removeChild(tmpNhat) ;
			var tmpNhi:MovieClip = MovieClip(mainStage_mc.getChildByName("Nhi"));
			if (tmpNhi != null)
				mainStage_mc.removeChild(tmpNhi) ;
			var tmpBa:MovieClip = MovieClip(mainStage_mc.getChildByName("Ba"));
			if (tmpBa != null)
				mainStage_mc.removeChild(tmpBa) ;
			var tmpBet:MovieClip = MovieClip(mainStage_mc.getChildByName("Bet"));
			while (tmpBet != null) {
				mainStage_mc.removeChild(tmpBet) ;
				tmpBet = MovieClip(mainStage_mc.getChildByName("Bet"));
			}
			var tmpMom:MovieClip = MovieClip(mainStage_mc.getChildByName("Mom"));
			while (tmpMom != null) {
				mainStage_mc.removeChild(tmpMom) ;
				tmpMom = MovieClip(mainStage_mc.getChildByName("Mom"));
			}
			var tmpAn:MovieClip = MovieClip(mainStage_mc.getChildByName("An"));
			if (tmpAn != null)
				mainStage_mc.removeChild(tmpAn) ;
			var tmpU:MovieClip = MovieClip(mainStage_mc.getChildByName("IconU"));
			if (tmpU != null)
				mainStage_mc.removeChild(tmpU) ;
			var tmpDen:MovieClip = MovieClip(mainStage_mc.getChildByName("IconDen"));
			if (tmpDen != null)
				mainStage_mc.removeChild(tmpDen) ;
			mainStage_mc.btnAddCard.visible = false ;
			//mainStage_mc.btnInitGame.visible = false ;
			RemoveBtnInitGame() ;
			mainStage_mc.Card00.visible = true ;
			mainStage_mc.Card00.vien_mc.visible = false;
			mainStage_mc.Card00.gotoAndStop(0);
			if (levelID == 0)
				mainStage_mc.Card00.gotoAndStop(54) ;
			else 
				mainStage_mc.Card00.gotoAndStop(1) ;
			//}						
			//{Remove het cac quan bai cua van truoc
			for (i = 0; i < currTable.ArrPlayers.length; i++) 
			{
				currTable.ArrPlayers[i].ArrCard = new Array() ;
				if (currTable.ArrPlayers[i].ArrMovie != null)
				while (currTable.ArrPlayers[i].ArrMovie.length != 0) 
					mainStage_mc.removeChild(currTable.ArrPlayers[i].ArrMovie.pop()) ;
				currTable.ArrPlayers[i].ArrCardRemove = new Array() ;
				if (currTable.ArrPlayers[i].ArrMovieRemove != null)
				while (currTable.ArrPlayers[i].ArrMovieRemove.length != 0) 
					mainStage_mc.removeChild(currTable.ArrPlayers[i].ArrMovieRemove.pop()) ;
				currTable.ArrPlayers[i].ArrCardDow = new Array() ;
				if (currTable.ArrPlayers[i].ArrMovieDow != null)				
				while (currTable.ArrPlayers[i].ArrMovieDow.length != 0) {
					var arr : Array = currTable.ArrPlayers[i].ArrMovieDow.pop() ;
					while (arr.length != 0)
						mainStage_mc.removeChild(arr.pop()) ;
				}
			}
			//}
			//{Khoi tao lai van bai
			for (j = 0; j < currTable.ArrPlayers.length; j++) 
			{
				currTable.ArrPlayers[j].ArrCard = new Array() ;
				currTable.ArrPlayers[j].ArrCardRemove = new Array() ;
				currTable.ArrPlayers[j].ArrCardDow = new Array() ;
				currTable.ArrPlayers[j].ArrMovie = new Array() ;
				currTable.ArrPlayers[j].ArrMovieRemove = new Array() ;
				currTable.ArrPlayers[j].ArrMovieDow = new Array() ;	
				currTable.ArrPlayers[j].MarkTakeInGame = 0 ;
			}	
			strGCOld = "" ;
			strHCOld = "" ;
			strSendOld = "" ;			
			currTable.Status = 1 ; //Chuyen trang thai ==> Dang choi
			isU = false ;
			currentAction = "Danh" ;
			//}
			ResetXYValue() ;
			var object = JSON.decode(strLC) ;
			var userStart : String = object.NName ;
			var csStart : int = 0 ;
			for (j = 0; j < currTable.ArrPlayers.length; j++) 
			{
				if (currTable.ArrPlayers[j].PlayerName == userStart) {
					csStart = j ;
					break ;
				}
			}			
			//tf.appendText("Start:" + csStart) ;
			var numberPlayer:Number = currTable.CurrentPlayer;			
			var arrLC :Array = object.ArrCard ; // (JSON.decode(strLC.substring(1)) as Array) //strLC.split(";") ;
			tChiaBai = new Timer(20) ;			
			tChiaBai.addEventListener(TimerEvent.TIMER, Start);			
			var dem:Number = 0;
			tChiaBai.repeatCount = 9 * numberPlayer + 1;
			tChiaBai.start();					
			var cs:int = -1 ;
			/* Tim vi tri dau tien*/
			dem = csStart ;			
			function Start(evt:TimerEvent)
			{
				var numObj:Number = dem % numberPlayer;
				var tmpCard : MovieClip ;
				if (currPosition == numObj)
					tmpCard = MovieClip(new card()) ;
				else 
					tmpCard = MovieClip(new card()) ;
				tmpCard.x = 367;
				tmpCard.y = 227;		
				
				tmpCard.vien_mc.visible = false ;
				var objCard : Card = new Card() ;
				objCard.NumberCard = 0 ;
				objCard.Suit = 0 ;
				if (numObj == currPosition) {
					cs = cs + 1 ;										
					objCard.Suit = arrLC[cs].S ; 
					objCard.NumberCard = arrLC[cs].N; 
				}
				var position :Number = objCard.NumberCard + (objCard.Suit - 1) * 13 + 1 ;
				objCard.State = 0 ;				
				objCard.MoviePosition = numObj ;
				mainStage_mc.addChild(tmpCard);
				if (numObj != currPosition){
					position = 1 ;
					if (levelID == 0)
						position = 54 ;
				}
				if ((currPosition - numObj == 2) || (currPosition - numObj == -2)) {
					moveObj(tmpCard, positionCards[2].x0 , positionCards[2].y0, numObj, position);
					objCard.X = positionCards[2].x0 ;
					objCard.Y = positionCards[2].y0 ;
					positionCards[2].x0 += 15
				} else if ((currPosition - numObj == 1) || (currPosition - numObj == -3)) {
					moveObj(tmpCard, positionCards[3].x0, positionCards[3].y0, numObj, position);
					objCard.X = positionCards[3].x0 ;
					objCard.Y = positionCards[3].y0 ;
					positionCards[3].y0 += 20;
				} else if ((currPosition - numObj == -1) || (currPosition - numObj == 3)) {
					moveObj(tmpCard, positionCards[1].x0 , positionCards[1].y0, numObj, position);
					objCard.X = positionCards[1].x0 ;
					objCard.Y = positionCards[1].y0 ;
					positionCards[1].y0 += 20;
				} else {
					moveObj(tmpCard, positionCards[0].x0 , positionCards[0].y0, numObj, position);
					objCard.X = positionCards[0].x0 ;
					objCard.Y = positionCards[0].y0 ;
					positionCards[0].x0 += 20;
				}
				if (currTable.ArrPlayers[numObj].ArrCard == null) currTable.ArrPlayers[numObj].ArrCard = new Array() ;
				if (currTable.ArrPlayers[numObj].ArrMovie == null) currTable.ArrPlayers[numObj].ArrMovie = new Array() ;
				currTable.ArrPlayers[numObj].ArrCard.push(objCard) ;
				objCard = null ;
				dem++;
				if (tChiaBai.currentCount == tChiaBai.repeatCount) { //Thoi gian chia bai ket thuc ==> Khoi tao thoi gian dem danh bai
					if (!mainTimer.running) {
						mainTimer = new Timer(1000) ;
						mainTimer.repeatCount = 20 ;					
						mainTimer.addEventListener(TimerEvent.TIMER, StartTimeMain);		
						mainTimer.start() ;
						currentTimerRun = csStart ;
						if ((csStart == currPosition) && (!isView)) {
							mainStage_mc.btnDanhbai.addEventListener(MouseEvent.CLICK, clickRemoveCard) ;
							mainStage_mc.btnDanhbai.gotoAndStop(1) ;
						}
					}
				}
			}
		}
		// Hàm tạo chuyển động cho movie đến vị trí mới, thời gian 1s
		public static function moveObj(_mc: MovieClip, xNew:Number, yNew:Number, numObj : Number, position:Number)	{				
			TweenMax.to(_mc, 0.8, { x:xNew, y:yNew, ease:Expo.easeOut, onComplete:Latbai() } );
			function Latbai()
			{
				_mc.gotoAndStop(position);
				if (currTable.ArrPlayers[numObj].ArrMovie == null) currTable.ArrPlayers[numObj].ArrMovie = new Array() ;
				currTable.ArrPlayers[numObj].ArrMovie.push(_mc) ;
				_mc = null ;				
				/*if (numObj == currPosition) {	
					_mc.gotoAndStop(position);		
					if (currTable.ArrPlayers[currPosition].ArrMovie == null) currTable.ArrPlayers[currPosition].ArrMovie = new Array() ;
					currTable.ArrPlayers[currPosition].ArrMovie.push(_mc) ;
					var objCard : Card = new Card() ;
					objCard.NumberCard = (position -1) % 13;
					if (objCard.NumberCard == 0) objCard.NumberCard = 13 ;
					if (objCard.NumberCard == 13)
						objCard.Suit = Math.floor((position - 1) / 13);
					else
						objCard.Suit = Math.floor((position - 1) / 13) + 1;
					objCard.State = 0 ;
					objCard.X = xNew ;
					objCard.Y = yNew ;
					objCard.MoviePosition = numObj ;
					if (currTable.ArrPlayers[currPosition].ArrCard == null) currTable.ArrPlayers[currPosition].ArrCard = new Array() ;
					currTable.ArrPlayers[currPosition].ArrCard.push(objCard) ;					
				}*/
			}
		}
		/*
		 * Khoi tao thong tin ban
		 * ==> Add thong tin user vào vi tri 0 và hien thi
		 */
		public static function CreateTable(str : String):void {
			AllowExit = true ;
			//tf.appendText("==>1") ;
			isView = false ;	
			var tmpHide:MovieClip = MovieClip(mainStage_mc.getChildByName("HideCT"));			
			while (tmpHide != null) {
				mainStage_mc.removeChild(tmpHide) ;
				tmpHide = MovieClip(mainStage_mc.getChildByName("HideCT"));
			}
			var tmpLD:MovieClip = MovieClip(mainStage_mc.getChildByName("LoadingCT"));
			while (tmpLD != null) {
				mainStage_mc.removeChild(tmpLD) ;
				tmpLD = MovieClip(mainStage_mc.getChildByName("LoadingCT"));
			}
			//tf.appendText("\n Create table user info:" + str) ;
			ResetXYValue() ;
			//{ Khoi tao giao dien
			mainStage_mc.gotoAndStop(5);
			if (mainStage_mc.BG_mc.numChildren > 1)
				mainStage_mc.BG_mc.removeChildAt(1) ;
			if (levelID == 0) {						
				if (ArrBG[7] != null)
					mainStage_mc.BG_mc.addChild(ArrBG[7]) ;	
			} else {
				if (ArrBG[8] != null)
					mainStage_mc.BG_mc.addChild(ArrBG[8]) ;	
			}			
			mainStage_mc.btnVolumn.addEventListener(MouseEvent.CLICK, clickVolumn) ;
			mainStage_mc.btnXepbai.addEventListener(MouseEvent.CLICK, clickSortCard) ;
			mainStage_mc.btnAddCard.addEventListener(MouseEvent.CLICK, clickAddCard) ;
			mainStage_mc.btnChat_mc.addEventListener(MouseEvent.CLICK, clickChat) ;
			//mainStage_mc.btnReady.addEventListener(MouseEvent.CLICK, clickReadyTable) ;			
			//mainStage_mc.btnInitGame.addEventListener(MouseEvent.CLICK, clickStartTable) ;
			AddBtnInitGame() ;
			AddBtnReady() ;
			mainStage_mc.btnBackTable.addEventListener(MouseEvent.CLICK, clickExitTable) ;	
			mainStage_mc.txtChat_mc.addEventListener(KeyboardEvent.KEY_DOWN, keyDowChat) ;
			if (TimerInTable.running)
				TimerInTable.stop() ;
			TimerInTable = new Timer(1000) ;
			TimerInTable.repeatCount = 2 ;			
			TimerInTable.addEventListener(TimerEvent.TIMER, StartTimerInTable);			
			TimerInTable.start() ;
			if (isSound) {
				mainStage_mc.btnVolumn.gotoAndStop("active") ;
			} else {
				mainStage_mc.btnVolumn.gotoAndStop("inactive") ;
			}
			mainStage_mc.PlayerTop_mc.visible = false ;
			mainStage_mc.PlayerRight_mc.visible = false ;			
			mainStage_mc.PlayerLeft_mc.visible = false ;
			mainStage_mc.btnAddCard.visible = false ;
			//mainStage_mc.btnInitGame.visible = false ;
			RemoveBtnInitGame() ;
			mainStage_mc.Card00.visible = false ;	
			mainStage_mc.btnBackTable.visible = false ;
			mainStage_mc.btnHabai.gotoAndStop(2) ;
			mainStage_mc.btnGuibai.gotoAndStop(2) ;
			mainStage_mc.btnDanhbai.gotoAndStop(2) ;
			mainStage_mc.btnDanhbai.removeEventListener(MouseEvent.CLICK, clickRemoveCard) ;
			//}
			//}
			var object = JSON.decode(str) ;
			currTable = new ObjectTable() ;
			currTable.TableID = object.Id ;
			currTable.CurrentPlayer = 1 ;//object.CurPlay ;
			currTable.MarkUnit = object.MarkUnit ;
			currTable.TableName = object.Name ;
			currTable.Status = 0 ;
			currPlayer.CurrTableID = object.Id ;
			currPlayer.IsCreateTable = true ;
			currPlayer.Ready = false ;
			currPlayer.Type = 1 ; //La nguoi san sang choi
			currTable.ArrPlayers = new Array() ;
			currTable.ArrPlayers.push(currPlayer) ;			
			mainStage_mc.MarkUnit_txt.text = currTable.MarkUnit + " -- Bàn:" + currTable.TableID;
			currPosition = 0 ;
			LoadData_Bottom(currTable.ArrPlayers[0].PlayerName, currTable.ArrPlayers[0].Mark, currTable.ArrPlayers[0].PlayerIP, currTable.ArrPlayers[0].TinyURL, false) ;			
			DisplayColorAgain() ;
			mainSocket.sendData("RG:" + currTable.TableID + "ID:" + user.PlayId);			
		}
		
		/**************************************** Boc Bai *********************************************************/
		/*
		 * Click Boc bai ==> Gui du lieu thong bao len cho Server biet
		 */
		public static function clickAddCard(evt : MouseEvent) {
			AddCard() ;
		}
		public static function PlaySoundAdd(event : Event) {
			addSound.play() ;
		}		
		public static function AddCard() {
			if (currTable.TableID == 0) return ;
			//{ Tao Tieng
			if (isSound) {
				try
				{
					if (addSound == null) {
						addSound = new Sound(new URLRequest(preURLSound + "Add.mp3"));
						addSound.addEventListener(Event.COMPLETE, PlaySoundAdd) ;
					} else {
						addSound.play()
					}
				} catch (e:Error) { }
			}
			//}
			//Remove button An
			//var tmpAn:MovieClip = MovieClip(mainStage_mc.getChildByName("An"));
			//if (tmpAn != null)
				//mainStage_mc.removeChild(tmpAn) ;
			//mainStage_mc.btnAddCard.visible = false ;					
			var strSend : String = "BC:" + currTable.TableID + "ID:" + user.PlayId ;
			mainSocket.sendData(strSend) ;
		}
		/* Boc bai
		 * ==> Them 1 con bai nhan duoc vao dung vi tri
		 * */
		public static function OtherAddCard(str : String) {
			tf.appendText("\n Add card: " +str)
			var objCard : Card = new Card() ; //Con bài thêm vào danh sách
			//{ Phan tich cap nhat quan bai va hien thi quan bai tren giao dien
			var object = JSON.decode(str) ;
			var cs : int = 0;
			var name : String = object.Name ;
			for (var j:int = 0; j < currTable.ArrPlayers.length; j++) 
			{
				if (currTable.ArrPlayers[j].PlayerName == name) {
					cs = j ;
					break ;
				}				
			}
			var totalCardRemove : int =  0  ;
			for (var l:int = 0; l < currTable.ArrPlayers.length; l++) 
			{
				if (currTable.ArrPlayers[l].ArrCardRemove != null)
					totalCardRemove = totalCardRemove + currTable.ArrPlayers[l].ArrCardRemove.length ;
			}
			if (totalCardRemove == currTable.ArrPlayers.length * 4 -1) 
				mainStage_mc.Card00.visible = false ;
			objCard.NumberCard = object.N ;
			objCard.Suit = object.S ;
			objCard.State = 0 ;
			objCard.X = currTable.ArrPlayers[cs].ArrCard[currTable.ArrPlayers[cs].ArrCard.length - 1].X ;
			objCard.Y = currTable.ArrPlayers[cs].ArrCard[currTable.ArrPlayers[cs].ArrCard.length - 1].Y ;
			if ((currPosition - cs == 0) || (currPosition - cs == 2) || (currPosition - cs == -2))
				objCard.X = objCard.X + 20 ;
			else 
				objCard.Y = objCard.Y + 28 ;
			currTable.ArrPlayers[cs].ArrCard.push(objCard) ; //Add Vao danh sach bai cua user cs ;
			var tmpCard : MovieClip ;
			if (currPosition == cs)
				tmpCard = MovieClip(new card()) ;
			else 
				tmpCard = MovieClip(new card()) ;
			tmpCard.x = 367;
			tmpCard.y = 227;
			tmpCard.vien_mc.visible = false ;
			if (currPosition == cs)
				new CardController(tmpCard, objCard) ;				
			mainStage_mc.addChild(tmpCard);	
			moveCard(tmpCard, objCard.X, objCard.Y) ;
			if ((currPosition == cs) && (isView == false))
				tmpCard.gotoAndStop(objCard.NumberCard + (objCard.Suit - 1) * 13 + 1) ;
			else {
				if (levelID == 0)
					tmpCard.gotoAndStop(54) ;
				else 
					tmpCard.gotoAndStop(1) ;
			}
			currTable.ArrPlayers[cs].ArrMovie.push(tmpCard) ;//Add Movie vao danh sach Movie bai cua player cs
			//}
			//tf.appendText("==>Hien thi giao dien:" + cs) ;
			//{Check xem co u luon duoc khong neu cs == currentPosition
			if ((currPosition == cs) && (isView == false)) {
				var tmpAn:MovieClip = MovieClip(mainStage_mc.getChildByName("An"));
				if (tmpAn != null)
					mainStage_mc.removeChild(tmpAn) ;
				mainStage_mc.btnAddCard.visible = false ;
				
				ArrState = new Array() ;
				playerCards = null ;
				playerCards = new Array() ;
				//Khoi tao cho mang ArrState 0 - 0 vao phom, 1 vao phom doc, 2 vao phom ngang - 3 vao ca phom doc va phom ngang
				for (var k:int = 0; k < currTable.ArrPlayers[currPosition].ArrCard.length; k++)
					playerCards.push(currTable.ArrPlayers[currPosition].ArrCard[k]) ;
				playerCards.sortOn(["NumberCard", "Suit"], [Array.NUMERIC, Array.NUMERIC]);
				//Khoi tao cho mang ArrState 0 - 0 vao phom, 1 vao phom doc, 2 vao phom ngang - 3 vao ca phom doc va phom ngang
				for (var i:int = 0; i < playerCards.length; i++) 
				{
					var isNgang : Boolean = IsNumberCard(i) ;
					var isDoc : Boolean = IsSuit(i) ;
					//trace("ABC:" + currTable.ArrPlayers[currPosition].ArrCard[i].NumberCard + currTable.ArrPlayers[currPosition].ArrCard[i].Suit + "-" + isNgang + "-" + isDoc);
					if (isNgang) {
						if (isDoc) 
							ArrState.push("3") ;						
						else 
							ArrState.push("2") ;					
					} else if (isDoc) 
						ArrState.push("1") ;					
					else 
						ArrState.push("0") ;					
				}				
				CountSelection = 0 ;
				MinCard = 10 ;
				//Goi ham De Quy			
				Recursive(0) ;
				if (MinCard < 2) {
					SortForNumber() ;
					if (currTable.ArrPlayers[currPosition].ArrCard.length < 9)
						AutoDownCard("HC") ;
					else
						AutoDownCard("HU") ;
					//AutoDownCard("HU") ;
					isU = true ;
				}
			}
			//}
			//tf.appendText("==>is U:" + isU) ;
			if (isU) return ;
			//tf.appendText("==>Danh") ;
			//{Khoi tao lai thoi gian + Kiem tra de enable Habai + Gui bai
			if (mainTimer.running)
				mainTimer.stop() ;
			mainTimer = new Timer(1000) ;
			if ((currPosition == cs) && (!isView)) {
				mainStage_mc.btnDanhbai.gotoAndStop(1) ;
				mainStage_mc.btnDanhbai.addEventListener(MouseEvent.CLICK, clickRemoveCard) ;
			}
			//tf.appendText("==>" + currTable.ArrPlayers[currPosition].ArrCardRemove.length + "-" + currPosition + "-" + cs + "\n") ;
			if ((currTable.ArrPlayers[currPosition].ArrCardRemove.length == 3) && (currPosition == cs) && (isView == false)) {
				//Hide
				var tmpHide:MovieClip = MovieClip(new Hide());
				tmpHide.name = "Hide";
				tmpHide.x = 0 ;
				tmpHide.y = 0 ;
				tmpHide.alpha = 0 ;
				mainStage_mc.addChild(tmpHide) ;
				
				var tmpTable:MovieClip = MovieClip(new ChonHa_Popup());
				if (ArrPopupBG[2] != null)
					tmpTable.BG_mc.addChild(ArrPopupBG[2]) ;
				tmpTable.BG_mc.width = 210 ;
				tmpTable.BG_mc.height = 120 ;
				tmpTable.name = "ChonHa_Popup";
				tmpTable.x = 290;
				tmpTable.y = 225;
				tmpTable.btnOK_mc.addEventListener(MouseEvent.CLICK, btnOK_mc_ChonHa_click);
				mainStage_mc.addChild(tmpTable);
				mainStage_mc.btnGuibai.addEventListener(MouseEvent.CLICK, clickAttachCard) ;
				mainStage_mc.btnGuibai.gotoAndStop(1) ;	
				mainTimer.repeatCount = 5 ;				
				currentAction = "AutoHa" ;
			} else if (currTable.ArrPlayers[cs].ArrCardRemove.length == 3){		
				mainTimer.repeatCount = 35 ;
				currentAction = "Danh" ;				
			} else {
				mainTimer.repeatCount = 20 ;				
				currentAction = "Danh" ;				
			}
			mainTimer.addEventListener(TimerEvent.TIMER, StartTimeMain);				
			mainTimer.start() ;
			//}
		}
		
		/*************************************** Ha phom *********************************************************/
		/*
		 * User Click Ha phom
		 * ==> Chon cac con bai can ha trong moi phom
		 * ==> Dieu khien Hien thi tren giao dien
		 * ==> Gui thong tin cho Server
		 */
		public static function clickDownCard(evt : MouseEvent) {
			DownCard() ;			
		}
		
		public static function DownCardUDen(str : String) {
			//tf.appendText("==>str") ;
			var object = JSON.decode(str);
			var arrTemp : Array = object.ArrCard ;
			var t:Boolean = false ;
			var i :int ;
			for (var j:int = 0; j < arrTemp.length; j++) 
			{
				for (i = 0; i < currTable.ArrPlayers[currPosition].ArrCard.length; i++) 
				{
					if (currTable.ArrPlayers[currPosition].ArrCard[i].Suit != arrTemp[j].S) continue ;
					if (currTable.ArrPlayers[currPosition].ArrCard[i].NumberCard != arrTemp[j].N) continue ;
					t = true ;
					for (var k:int = i+1; k < currTable.ArrPlayers[currPosition].ArrCard.length; k++) 
					{
						//Giam toa do cac con sau con da nhan
						currTable.ArrPlayers[currPosition].ArrCard[k].X = currTable.ArrPlayers[currPosition].ArrCard[k].X - 20;							
						currTable.ArrPlayers[currPosition].ArrMovie[k].x = currTable.ArrPlayers[currPosition].ArrMovie[k].x - 20 ;						
					}
					mainStage_mc.removeChild(currTable.ArrPlayers[currPosition].ArrMovie[i]) ; //Remove khoi giao dien
					currTable.ArrPlayers[currPosition].ArrMovie.splice(i, 1) ; //Remove khoi danh sach Movie
					currTable.ArrPlayers[currPosition].ArrCard.splice(i, 1) ; //Remove khoi danh sach Movie
					break ;
				}				
			}
			if (!t) return ;			
			//tf.appendText("Ha Phom:" + currTable.ArrPlayers[currPosition].ArrCard.length) ;
			showCard(currTable.ArrPlayers[currPosition].ArrCard) ;
			//isU = true ;
			var tmp:MovieClip = MovieClip(mainStage_mc.getChildByName("ChonHa_Popup")) ;
			if (tmp != null)
				mainStage_mc.removeChild(tmp) ;			
			//if (mainTimer.running)
			//	mainTimer.stop() ;
			//Ha bai
			if (currTable.ArrPlayers[currPosition].ArrCardDow == null) currTable.ArrPlayers[currPosition].ArrCardDow = new Array() ;
			if (currTable.ArrPlayers[currPosition].ArrMovieDow == null) currTable.ArrPlayers[currPosition].ArrMovieDow = new Array() ;
			//Hien thi danh sach bai ha
			var arrCardTemp : Array = new Array() ;
			var arrMovieTemp : Array = new Array() ;
			for (i = 0; i < arrTemp.length; i++) 
			{
				var objCard : Card = new Card() ;
				objCard.NumberCard = arrTemp[i].N ;
				objCard.Suit = arrTemp[i].S ;
				//tf.appendText("==>ABC:" + objCard.NumberCard + "-" + objCard.Suit) ;
				objCard.State = 2 ;
				objCard.X = positionCards[8].x0 + i * 18;
				objCard.Y = positionCards[8].y0 + currTable.ArrPlayers[currPosition].ArrCardDow.length * 28;
				arrCardTemp.push(objCard) ;
				var positionTimeLine : Number = arrTemp[i].N + (arrTemp[i].S - 1) * 13 + 1 ;
				var tmpCard : MovieClip = MovieClip(new card()) ;
				tmpCard.x = objCard.X;
				tmpCard.y = objCard.Y;
				tmpCard.vien_mc.visible = false ;
				mainStage_mc.addChild(tmpCard) ;
				tmpCard.gotoAndStop(positionTimeLine);
				arrMovieTemp.push(tmpCard) ;
			}
			currTable.ArrPlayers[currPosition].ArrMovieDow.push(arrMovieTemp) ; 
			currTable.ArrPlayers[currPosition].ArrCardDow.push(arrCardTemp) ; 
		}
		
		public static function DownCard() {
			if (currTable.TableID == 0) return ;
			var arrCardCheck : Array = new Array() ;
			var t:Boolean = true ;
			var i : int ;
			for (var j:int = 0; j < currTable.ArrPlayers[currPosition].ArrCard.length; j++) 
			{
				if ((currTable.ArrPlayers[currPosition].ArrCard[j].X != currTable.ArrPlayers[currPosition].ArrMovie[j].x) || (currTable.ArrPlayers[currPosition].ArrCard[j].Y != currTable.ArrPlayers[currPosition].ArrMovie[j].y)) {
					arrCardCheck.push(currTable.ArrPlayers[currPosition].ArrCard[j]) ;
				}
			}
			var demTake : int = 0 ;
			for (var m:int = 0; m < arrCardCheck.length; m++) 
			{
				if (arrCardCheck[m].State == 2) demTake ++ ;				
			}
			if (demTake > 1) return ;
			//Check xem phom ha co hop le khong
			var fc : BasicFunction = new BasicFunction() ;
			fc.arrCard = arrCardCheck ;
			fc.SortForNumber() ;
			if (!fc.Check_Phom(fc.arrCard)) return ;			
			//{ Cap nhat lai giao dien va toa do cac quan bai khac con tren tay
			/*while (t) {
				t = false ;
				for (var k:int = 0; k < currTable.ArrPlayers[currPosition].ArrCard.length; k++) 
				{
					if ((currTable.ArrPlayers[currPosition].ArrCard[k].X != currTable.ArrPlayers[currPosition].ArrMovie[k].x) || (currTable.ArrPlayers[currPosition].ArrCard[k].Y != currTable.ArrPlayers[currPosition].ArrMovie[k].y)) {
						for (i = k+1; i < currTable.ArrPlayers[currPosition].ArrCard.length; i++) 
						{
							currTable.ArrPlayers[currPosition].ArrCard[i].X = currTable.ArrPlayers[currPosition].ArrCard[i].X - 20;							
							currTable.ArrPlayers[currPosition].ArrMovie[i].x = currTable.ArrPlayers[currPosition].ArrMovie[i].x - 20 ;
						}
						mainStage_mc.removeChild(currTable.ArrPlayers[currPosition].ArrMovie[k]) ; //Remove khoi giao dien
						currTable.ArrPlayers[currPosition].ArrMovie.splice(k, 1) ; //Remove khoi danh sach Movie
						currTable.ArrPlayers[currPosition].ArrCard.splice(k, 1) ; //Remove khoi danh sach Movie
						t = true ;
						break ;
					}
				}				
			}
			showCard(currTable.ArrPlayers[currPosition].ArrCard) ;*/
			//}
			//Gui du lieu len Server
			//if (currTable.ArrPlayers[currPosition].ArrCardDow == null) currTable.ArrPlayers[currPosition].ArrCardDow = new Array() ;
			//if (currTable.ArrPlayers[currPosition].ArrMovieDow == null) currTable.ArrPlayers[currPosition].ArrMovieDow = new Array() ;
			//Hien thi danh sach bai ha
			var strSend : String ; 
			strSend = "HC:" + currTable.TableID + "CR:";
			var arrCardTemp : Array = new Array() ;
			var arrMovieTemp : Array = new Array() ;
			for (i = 0; i < arrCardCheck.length; i++) 
			{
				var objCard : Card = new Card() ;
				objCard.NumberCard = arrCardCheck[i].NumberCard ;
				objCard.Suit = arrCardCheck[i].Suit ;
				objCard.State = arrCardCheck[i].State ;
				objCard.X = positionCards[8].x0 + i * 18;
				objCard.Y = positionCards[8].y0 + currTable.ArrPlayers[currPosition].ArrCardDow.length * 28;
				/*arrCardTemp.push(objCard) ;
				var positionTimeLine : Number = arrCardCheck[i].NumberCard + (arrCardCheck[i].Suit - 1) * 13 + 1 ;
				var tmpCard : MovieClip = MovieClip(new card()) ;
				tmpCard.x = objCard.X;
				tmpCard.y = objCard.Y;
				mainStage_mc.addChild(tmpCard) ;
				tmpCard.gotoAndStop(positionTimeLine);
				arrMovieTemp.push(tmpCard) ;*/	
				strSend = strSend + objCard.Suit + objCard.NumberCard + "_" ;
			}
			//currTable.ArrPlayers[currPosition].ArrMovieDow.push(arrMovieTemp) ; //[currTable.ArrPlayers[currPosition].ArrMovieDow.length] = new Array() ;
			//currTable.ArrPlayers[currPosition].ArrCardDow.push(arrCardTemp) ; //[currTable.ArrPlayers[currPosition].ArrCardDow.length] = new Array() ;
			//Check xem trong phom co 2 cay an khong
			demTake = 0 ;
			for (var l:int = 0; l < arrCardTemp.length; l++) 
			{
				if (arrCardTemp[l].State == 2) 
					demTake++ ;	
			}
			//Send Data len server
			strSend = strSend.substring(0, strSend.length -1) ;
			strSend = strSend + "ID:" + user.PlayId ;
			mainSocket.sendData(strSend + "^") ;
			/*if (demTake > 1) {
				mainSocket.sendData("DL:" + currTable.TableID + "ID:" + user.PlayId) ;				
			}*/					
		}
		/*
		 * Nhan duoc thong tin ha phom cua cac Player khac tu Server
		 */
		public static function PlaySoundDown(event : Event) {
			downSound.play() ;
		}
		
		public static function OtherDownCard(str : String) {
			tf.appendText("\n Down card: " + str);
			//{ Tao Tieng
			if (isSound) {
				try
				{
					if (downSound == null) {
						downSound = new Sound(new URLRequest(preURLSound + "Down.mp3"));
						downSound.addEventListener(Event.COMPLETE, PlaySoundDown) ;
					} else {
						downSound.play()
					}
				} catch (e:Error) { }
			}
			//}
			var object = JSON.decode(str);			
			var cs : int = 0;
			var playerName : String = object.Name ;				
			for (var m:int = 0; m < currTable.ArrPlayers.length; m++) 
			{
				if (currTable.ArrPlayers[m].PlayerName == playerName) {
					cs = m ;
					break ;
				}				
			}
			if ((cs == currPosition) && (!isView)) {
				//{Remove con bai dang cam tren tay neu co
				var movieMove : MovieClip = MovieClip(mainStage_mc.getChildByName("MovieMove")) ;
				if (movieMove != null) {				
					isTranfer = false ;
					currTable.ArrPlayers[currPosition].ArrMovie[csMove].visible = true ;
					mainStage_mc.removeChild(movieMove) ;
				}
				//}
				DownCardUDen(str) ;
				return ;			
			}
			var arrTemp : Array = object.ArrCard ;
			if (currTable.ArrPlayers[cs].ArrCardDow == null) currTable.ArrPlayers[cs].ArrCardDow = new Array() ;
			var arr : Array = new Array() ;
			for (var j:int = 0; j < arrTemp.length; j++) 
			{
				var objCard : Card = new Card() ;
				objCard.NumberCard = arrTemp[j].N ;
				objCard.Suit = arrTemp[j].S ;
				objCard.State = 2 ;
				if ((currPosition - cs == 2) || (currPosition - cs == -2)) {
					objCard.X = positionCards[10].x0 + 18 * j ;
					objCard.Y = positionCards[10].y0 + 28 * currTable.ArrPlayers[cs].ArrCardDow.length ;
				} else if ((currPosition - cs == 3) || (currPosition - cs == -1)) {
					objCard.X = positionCards[9].x0 + 18 * j ;
					objCard.Y = positionCards[9].y0 + 28 * currTable.ArrPlayers[cs].ArrCardDow.length ;
				} else if ((currPosition - cs == -3) || (currPosition - cs == 1)) {
					objCard.X = positionCards[11].x0 + 18 * j ;
					objCard.Y = positionCards[11].y0 + 28 * currTable.ArrPlayers[cs].ArrCardDow.length ;
				} else {
					objCard.X = positionCards[8].x0 + 18 * j ;
					objCard.Y = positionCards[8].y0 + 28 * currTable.ArrPlayers[cs].ArrCardDow.length ;
				}
				arr.push(objCard) ;				
			}
			currTable.ArrPlayers[cs].ArrCardDow.push(arr) ; //Add vao danh sach ha bai cua Player cs
			if (currTable.ArrPlayers[cs].ArrMovieDow == null) currTable.ArrPlayers[cs].ArrMovieDow = new Array() ;
			var arrMovie : Array = new Array() ;
			for (var i:int = 0; i < arr.length; i++) 
			{
				var positionTimeLine : Number = arr[i].NumberCard + (arr[i].Suit - 1) * 13 + 1 ;
				var tmpCard : MovieClip = MovieClip(new card()) ;
				tmpCard.x = arr[i].X;
				tmpCard.y = arr[i].Y;
				tmpCard.vien_mc.visible = false ;
				tmpCard.gotoAndStop(positionTimeLine);
				mainStage_mc.addChild(tmpCard) ;				
				arrMovie.push(tmpCard) ;
			}
			currTable.ArrPlayers[cs].ArrMovieDow.push(arrMovie) ;
			//{Remove khoi danh sach bai cua Player cs 
			var dem : int = arr.length ;
			while (dem > 0) {
				var t : Boolean = false ;
				for (var l:int = 0; l < currTable.ArrPlayers[cs].ArrCard.length; l++) 
				{
					if (currTable.ArrPlayers[cs].ArrCard[l].NumberCard != 0) {
						mainStage_mc.removeChild(currTable.ArrPlayers[cs].ArrMovie[l]) ;
						currTable.ArrPlayers[cs].ArrCard.splice(l, 1) ;
						currTable.ArrPlayers[cs].ArrMovie.splice(l, 1) ;
						t = true ;
						dem-- ;
						break ;
					}				
				}
				if (!t) {
					for (var k:int = 0; k < dem; k++) 
					{
						var _mc : MovieClip = currTable.ArrPlayers[cs].ArrMovie.pop() ; 
						mainStage_mc.removeChild(_mc) ;
						currTable.ArrPlayers[cs].ArrCard.pop() ;
					}
					dem = 0 ;
				}
			}
			//}
		}
		
		/***************************************** Gui bai *********************************************************/
		public static function clickAttachCard(evt : MouseEvent) {
			AttachCard() ;
		}
		public static function AttachCard() {
			//{ Tao Tieng
			if (isSound) {
				try
				{
					if (removeSound == null) {
						removeSound = new Sound(new URLRequest(preURLSound + "Remove.mp3"));
						removeSound.addEventListener(Event.COMPLETE, PlaySoundRemove) ;
					} else {
						removeSound.play()
					}
				} catch (e:Error) { }
			}
			//}
			var arrAttach : Array = new Array() ;
			if (currTable.ArrPlayers[currPosition].ArrCard.length > 7) return ;
			//Ha tiep nhung con gui
			var t:Boolean = true ;
			while (t) {
				t = false ;
				for (var j:int = 0; j < currTable.ArrPlayers[currPosition].ArrCard.length; j++) 
				{
					if (currTable.ArrPlayers[currPosition].ArrCard[j].State == 2) continue ;
					if (currTable.ArrPlayers[currPosition].ArrCard[j].Y != currTable.ArrPlayers[currPosition].ArrMovie[j].y) {
						//Kiem tra xem co gui duoc khong
						var check : int = 0;
						var isGui : Boolean = false ; 
						var k:int ;
						var fc : BasicFunction ;
						var o, l, p, q : int ;
						var objC : Card ;
						var objCard : Card ;
						var xNew : Number ;
						var yNew : Number ;
						var _mc : MovieClip ;
						var positionTimeLine : Number ;
						var object : Object ;
						for (k = 0; k < currTable.ArrPlayers.length; k++) //Tim kiem de gui vao phom doc
						{
							//if (k == currPosition ) continue ;
							if (currTable.ArrPlayers[k].ArrCardDow != null)
								if (currTable.ArrPlayers[k].ArrCardDow.length > 0) {
									for (l = 0; l < currTable.ArrPlayers[k].ArrCardDow.length; l++) 
									{
										fc = new BasicFunction() ;
										fc.arrCard = new Array() ;
										for (o = 0; o < currTable.ArrPlayers[k].ArrCardDow[l].length; o++) 
										{
											objC = new Card() ;
											objC.NumberCard = currTable.ArrPlayers[k].ArrCardDow[l][o].NumberCard ;
											objC.Suit = currTable.ArrPlayers[k].ArrCardDow[l][o].Suit ;
											objC.State = currTable.ArrPlayers[k].ArrCardDow[l][o].State ;
											objC.X = currTable.ArrPlayers[k].ArrCardDow[l][o].X ;
											objC.Y = currTable.ArrPlayers[k].ArrCardDow[l][o].Y ;
											fc.arrCard.push(objC) ;
										}										
										xNew = fc.arrCard[fc.arrCard.length -1].X ;
										yNew = fc.arrCard[fc.arrCard.length -1].Y ;
										fc.arrCard.push(currTable.ArrPlayers[currPosition].ArrCard[j]) ;
										fc.SortForNumber() ;
										check = fc.Check_Phom(fc.arrCard) ;
										//trace("Check phom:" + k + "-" + l + "-" + check + "--" + currTable.ArrPlayers[currPosition].ArrCard[j].NumberCard + "-" + currTable.ArrPlayers[currPosition].ArrCard[j].Suit) ;
										if (check == 2) { //Gui duoc vao phom l cua player k ==> Phom doc
											objCard = new Card() ;
											objCard.NumberCard = currTable.ArrPlayers[currPosition].ArrCard[j].NumberCard ;
											objCard.Suit = currTable.ArrPlayers[currPosition].ArrCard[j].Suit ;
											objCard.State = 2 ;
											objCard.X = xNew + 18;
											objCard.Y = yNew;
											currTable.ArrPlayers[k].ArrCardDow[l].push(objCard) ;
											_mc = MovieClip(new card()) ;
											_mc.vien_mc.visible = false ;
											_mc.x = currTable.ArrPlayers[currPosition].ArrCard[j].X ; //objCard.X;
											_mc.y = currTable.ArrPlayers[currPosition].ArrCard[j].Y ; //objCard.Y;
											moveCard(_mc, objCard.X, objCard.Y) ;
											mainStage_mc.addChild(_mc) ;	
											//Hien thi lai cac con bai ben duoi
											for (p = l + 1; p < currTable.ArrPlayers[k].ArrCardDow.length; p++) 
											{
												for (q = 0; q < currTable.ArrPlayers[k].ArrCardDow[p].length; q++) 
												{
													mainStage_mc.removeChild(currTable.ArrPlayers[k].ArrMovieDow[p][q]) ;
													mainStage_mc.addChild(currTable.ArrPlayers[k].ArrMovieDow[p][q]) ;
												}												
											}
											positionTimeLine = objCard.NumberCard + (objCard.Suit - 1) * 13 + 1 ;
											_mc.gotoAndStop(positionTimeLine);
											currTable.ArrPlayers[k].ArrMovieDow[l].push(_mc) ;
											//Gui thong tin len Server
											object = new Object() ;
											object.N = objCard.NumberCard ;
											object.S = objCard.Suit ;
											object.Name = currTable.ArrPlayers[k].PlayerName ;
											object.Status = 0 ;
											arrAttach.push(object) ;
											isGui = true ;
											//strSend = strSend + "CR:" + objCard.Suit + objCard.NumberCard + "POS:" + currTable.ArrPlayers[k].PlayerName ;
											break ;
										}
									}
								}
							if (isGui) break ;
						}
						if (!isGui)
						for (k = 0; k < currTable.ArrPlayers.length; k++) //Gui phom ngang
						{
							//if (k == currPosition ) continue ;
							if (currTable.ArrPlayers[k].ArrCardDow != null)
								if (currTable.ArrPlayers[k].ArrCardDow.length > 0) {
									for (l = 0; l < currTable.ArrPlayers[k].ArrCardDow.length; l++) //Tim kiem de gui vao phom ngang
									{
										fc = new BasicFunction() ;
										fc.arrCard = new Array() ;
										for (o = 0; o < currTable.ArrPlayers[k].ArrCardDow[l].length; o++) 
										{
											objC = new Card() ;
											objC.NumberCard = currTable.ArrPlayers[k].ArrCardDow[l][o].NumberCard ;
											objC.Suit = currTable.ArrPlayers[k].ArrCardDow[l][o].Suit ;
											objC.State = currTable.ArrPlayers[k].ArrCardDow[l][o].State ;
											objC.X = currTable.ArrPlayers[k].ArrCardDow[l][o].X ;
											objC.Y = currTable.ArrPlayers[k].ArrCardDow[l][o].Y ;
											fc.arrCard.push(objC) ;
										}
										xNew = fc.arrCard[fc.arrCard.length -1].X ;
										yNew = fc.arrCard[fc.arrCard.length -1].Y ;
										fc.arrCard.push(currTable.ArrPlayers[currPosition].ArrCard[j]) ;
										fc.SortForNumber() ;
										check = fc.Check_Phom(fc.arrCard) ;
										if (check == 1) { //Gui duoc vao phom l cua player k ==> Phom ngang
											objCard = new Card() ;
											objCard.NumberCard = currTable.ArrPlayers[currPosition].ArrCard[j].NumberCard ;
											objCard.Suit = currTable.ArrPlayers[currPosition].ArrCard[j].Suit ;
											objCard.State = 2 ;
											objCard.X = xNew + 18;
											objCard.Y = yNew;
											currTable.ArrPlayers[k].ArrCardDow[l].push(objCard) ;
											_mc = MovieClip(new card()) ;
											_mc.vien_mc.visible = false; 
											_mc.x = currTable.ArrPlayers[currPosition].ArrCard[j].X ; //objCard.X;
											_mc.y = currTable.ArrPlayers[currPosition].ArrCard[j].Y ; //objCard.Y;
											moveCard(_mc, objCard.X, objCard.Y) ;
											mainStage_mc.addChild(_mc) ;
											//Hien thi lai cac con bai ben duoi
											for (p = l + 1; p < currTable.ArrPlayers[k].ArrCardDow.length; p++) 
											{
												for (q = 0; q < currTable.ArrPlayers[k].ArrCardDow[p].length; q++) 
												{
													mainStage_mc.removeChild(currTable.ArrPlayers[k].ArrMovieDow[p][q]) ;
													mainStage_mc.addChild(currTable.ArrPlayers[k].ArrMovieDow[p][q]) ;
												}												
											}
											positionTimeLine = objCard.NumberCard + (objCard.Suit - 1) * 13 + 1 ;
											_mc.gotoAndStop(positionTimeLine);
											currTable.ArrPlayers[k].ArrMovieDow[l].push(_mc) ;
											//Gui thong tin len Server
											object = new Object() ;
											object.N = objCard.NumberCard ;
											object.S = objCard.Suit ;
											object.Name = currTable.ArrPlayers[k].PlayerName ;
											object.Status = 0 ;
											arrAttach.push(object) ;
											isGui = true ;
											//strSend = strSend + "CR:" + objCard.Suit + objCard.NumberCard + "POS:" + currTable.ArrPlayers[k].PlayerName ;
											break ;
										}
									}
								}
							if (isGui) break ;
						}
						//trace("==>Gui duoc ==> Remove:" + j + "-" + currTable.ArrPlayers[currPosition].ArrMovie[j].X + "-" + currTable.ArrPlayers[currPosition].ArrMovie[j].Y) ;
						if (!isGui) continue ;
						//==> Gui duoc ==> Remove khoi danh sach bai tren tay ==> Gui thong tin len server
						for (var i:int = j+1; i < currTable.ArrPlayers[currPosition].ArrCard.length; i++) 
						{
							currTable.ArrPlayers[currPosition].ArrCard[i].X = currTable.ArrPlayers[currPosition].ArrCard[i].X - 20;							
							currTable.ArrPlayers[currPosition].ArrMovie[i].x = currTable.ArrPlayers[currPosition].ArrMovie[i].x - 20 ;
						}
						//trace("Card Attach:" + currTable.ArrPlayers[currPosition].ArrCard[j].NumberCard + currTable.ArrPlayers[currPosition].ArrCard[j].Suit) ;
						mainStage_mc.removeChild(currTable.ArrPlayers[currPosition].ArrMovie[j]) ; //Remove khoi giao dien
						currTable.ArrPlayers[currPosition].ArrMovie.splice(j, 1) ; //Remove khoi danh sach Movie
						currTable.ArrPlayers[currPosition].ArrCard.splice(j, 1) ; //Remove khoi danh sach Movie
						t = true ;
						break ;
					}
				}				
			}
			//trace("Attach==>Length:" + arrAttach.length) ;
			var strSend : String = "GC:" + currTable.TableID;
			t = true ;
			while (t) {
				t = false ;
				var strTemp : String = "" ;
				for (var m:int = 0; m < arrAttach.length; m++) 
				{
					if (arrAttach[m].Status == 0) {
						t = true ;
						strTemp = strTemp + arrAttach[m].S + arrAttach[m].N ;
						arrAttach[m].Status = 1 ;
						for (var n:int = m+1; n < arrAttach.length; n++) 
						{
							if ((arrAttach[n].Name == arrAttach[m].Name) && (arrAttach[n].Status == 0)) {
								strTemp = strTemp + "_" + arrAttach[n].S + arrAttach[n].N ;
								arrAttach[n].Status = 1 ;
							}
						}
						strTemp = strTemp + "POS:" + arrAttach[m].Name ;
						break ;
					}
				}
				if (t) strSend = strSend + "^" + strTemp ;	
			}
			strSend = strSend + "ID:" + user.PlayId ;
			//trace("Attach:" + strSend) ;
			if (strSend.indexOf("POS") > 0)
				mainSocket.sendData(strSend) ;			
			if (currTable.ArrPlayers[currPosition].ArrCard.length < 2) {
				mainSocket.sendData("FG:" + currTable.TableID + "ID:" + user.PlayId) ;
			}
		}
		public static function OtherAttachCard(str : String) {
			tf.appendText("\n Attach card " + str);
			var object = JSON.decode(str) ;
			var nname : String = object.NName ; //Thang nhan
			var name : String = object.Name ;//Thang gui
			var from : int = 0 ;
			var to : int = 0 ;			
			for (var l:int = 0; l < currTable.ArrPlayers.length; l++) 
			{
				if (currTable.ArrPlayers[l].PlayerName == name) {
					from = l ;
					break ;
				}					
			}
			for (var j:int = 0; j < currTable.ArrPlayers.length; j++) 
			{
				if (currTable.ArrPlayers[j].PlayerName == nname) {
					to = j ;
					break ;
				}					
			}
			if ((from == currPosition) && (!isView)) return ;
			//trace("OtherAttach:" + object.ArrCard.length) ;
			for (var i:int = 0; i < object.ArrCard.length; i++) 
			{
				var item = object.ArrCard[i] ;
				var objCard : Card = new Card() ;				
				objCard.NumberCard = item.N ;
				objCard.Suit = item.S ;
				objCard.State = 2 ;				
				//trace("OtherAttach==>Name:" + from + "-" + to) ;
				var isGui : Boolean = false ;
				var fc : BasicFunction = new BasicFunction() ;
				var xNew : Number = 0 ;
				var yNew : Number = 0 ;
				var objC : Card = new Card() ;
				var o,k,m,n:int = 0 ;	
				var _mc : MovieClip ;
				var check : int ;
				//Check phom doc
				for (k= 0; k < currTable.ArrPlayers[to].ArrCardDow.length; k++) 
				{
					fc = new BasicFunction() ;
					fc.arrCard = new Array() ;
					xNew = 0 ;
					yNew = 0 ;
					for (o = 0; o < currTable.ArrPlayers[to].ArrCardDow[k].length; o++) 
					{
						objC = new Card() ;
						objC.NumberCard = currTable.ArrPlayers[to].ArrCardDow[k][o].NumberCard ;
						objC.Suit = currTable.ArrPlayers[to].ArrCardDow[k][o].Suit ;
						objC.State = currTable.ArrPlayers[to].ArrCardDow[k][o].State ;
						objC.X = currTable.ArrPlayers[to].ArrCardDow[k][o].X ;
						objC.Y = currTable.ArrPlayers[to].ArrCardDow[k][o].Y ;
						if (objC.X > xNew) {
							//tf.appendText("==>AttachD:" + objC.X) ;
							xNew = objC.X ;
							yNew = objC.Y ;							
						}
						fc.arrCard.push(objC) ;
					}					
					//trace("XYNew:" + fc.arrCard.length + "-" + xNew + "-" + yNew) ;
					fc.arrCard.push(objCard) ;
					fc.SortForNumber() ;
					check = fc.Check_Phom(fc.arrCard) ;
					if (check == 2) {
						objCard.X = xNew + 18 ;
						objCard.Y = yNew ;
						_mc = MovieClip(new card()) ;
						_mc.x = objCard.X ;
						_mc.y = objCard.Y ;
						_mc.vien_mc.visible = false ;
						_mc.gotoAndStop(objCard.NumberCard + (objCard.Suit - 1) * 13 + 1) ;
						currTable.ArrPlayers[to].ArrCardDow[k].push(objCard) ;
						currTable.ArrPlayers[to].ArrMovieDow[k].push(_mc) ;
						mainStage_mc.addChild(_mc) ;
						//Remove khoi danh sach bai cua cs
						currTable.ArrPlayers[from].ArrCard.pop() ;
						mainStage_mc.removeChild(currTable.ArrPlayers[from].ArrMovie.pop()) ;
						isGui = true ;						
						//Hien thi lai nhung con gui cua phom cao hon cho do bi che
						for (m = k+1; m < currTable.ArrPlayers[to].ArrCardDow.length; m++) 
						{
							for (n = 0; n < currTable.ArrPlayers[to].ArrCardDow[m].length; n++) 
							{
								mainStage_mc.removeChild(currTable.ArrPlayers[to].ArrMovieDow[m][n]) ;
								mainStage_mc.addChild(currTable.ArrPlayers[to].ArrMovieDow[m][n]) ;
							}						
						}
						break ;
					}					
				}				
				//Check phom ngang
				if (!isGui)
				for (k = 0; k < currTable.ArrPlayers[to].ArrCardDow.length; k++) 
				{
					fc = new BasicFunction() ;
					fc.arrCard = new Array() ;
					xNew = 0 ;
					yNew = 0 ;
					for (o = 0; o < currTable.ArrPlayers[to].ArrCardDow[k].length; o++) 
					{
						objC = new Card() ;
						objC.NumberCard = currTable.ArrPlayers[to].ArrCardDow[k][o].NumberCard ;
						objC.Suit = currTable.ArrPlayers[to].ArrCardDow[k][o].Suit ;
						objC.State = currTable.ArrPlayers[to].ArrCardDow[k][o].State ;
						objC.X = currTable.ArrPlayers[to].ArrCardDow[k][o].X ;
						objC.Y = currTable.ArrPlayers[to].ArrCardDow[k][o].Y ;
						if (objC.X > xNew) {
							//tf.appendText("==>AttachN:" + objC.X) ;
							xNew = objC.X ;
							yNew = objC.Y ;
						}
						fc.arrCard.push(objC) ;
					}
					//trace("XYNew:" + fc.arrCard.length + "-" + xNew + "-" + yNew) ;
					fc.arrCard.push(objCard) ;
					fc.SortForNumber() ;
					check = fc.Check_Phom(fc.arrCard) ;
					if (check == 1) {
						objCard.X = xNew + 18 ;
						objCard.Y = yNew ;
						_mc = MovieClip(new card()) ;
						_mc.x = objCard.X ;
						_mc.y = objCard.Y ;
						_mc.vien_mc.visible = false ;
						_mc.gotoAndStop(objCard.NumberCard + (objCard.Suit - 1) * 13 + 1) ;
						currTable.ArrPlayers[to].ArrCardDow[k].push(objCard) ;
						currTable.ArrPlayers[to].ArrMovieDow[k].push(_mc) ;
						mainStage_mc.addChild(_mc) ;
						//Remove khoi danh sach bai cua cs
						currTable.ArrPlayers[from].ArrCard.pop() ;
						mainStage_mc.removeChild(currTable.ArrPlayers[from].ArrMovie.pop()) ;
						isGui = true ;
						//Hien thi lai nhung con gui cua phom cao hon cho do bi che
						for (m = k+1; m < currTable.ArrPlayers[to].ArrCardDow.length; m++) 
						{
							for (n = 0; n < currTable.ArrPlayers[to].ArrCardDow[m].length; n++) 
							{
								mainStage_mc.removeChild(currTable.ArrPlayers[to].ArrMovieDow[m][n]) ;
								mainStage_mc.addChild(currTable.ArrPlayers[to].ArrMovieDow[m][n]) ;
							}						
						}
						break ;
					}
				}
			}			
		}
		
		/*************************************** Danh bai *******************************************************/
		/*
		 * Player kich vao nut danh bai
		 * ==> Dieu khien hien thi giao dien
		 * ==> Gui thong tin con bai danh cho Server
		 */
		public static function clickRemoveCard(evt : MouseEvent) {			
			if (currentAction != "Danh") return ;			
			RemoveCard() ;			
		}
		public static function RemoveCard() {
			if (currTable.TableID == 0) return ;
			var dem : int = 0 ;
			for (var m:int = 0; m < currTable.ArrPlayers[currPosition].ArrCard.length; m++) 
			{
				if ((currTable.ArrPlayers[currPosition].ArrCard[m].Y != currTable.ArrPlayers[currPosition].ArrMovie[m].y)
					&& (currTable.ArrPlayers[currPosition].ArrCard[j].State != 2)) dem++ ;				
			}
			if (dem > 1) return ;			
			for (var j:int = 0; j < currTable.ArrPlayers[currPosition].ArrCard.length; j++) 
			{				
				if ((currTable.ArrPlayers[currPosition].ArrCard[j].Y != currTable.ArrPlayers[currPosition].ArrMovie[j].y)
					&& (currTable.ArrPlayers[currPosition].ArrCard[j].State != 2)) {
						var strSend : String = "DC:" + currTable.TableID + "CR:" + currTable.ArrPlayers[currPosition].ArrCard[j].Suit + currTable.ArrPlayers[currPosition].ArrCard[j].NumberCard + "ID:" + user.PlayId ;
						mainSocket.sendData(strSend) ;
						break ;	
					}			
			}
		}
		/*
		 * Danh Bai
		 * Input: str cho biet user nao danh bai va danh con bai gi
		 */
		public static function PlaySoundRemove(event : Event) {
			removeSound.play() ;
		}
		
		public static function OtherRemoveCard(str : String) {
			tf.appendText("\n Remove card: " + str);
			//{Remove con bai dang cam tren tay neu co
			var movieMove : MovieClip = MovieClip(mainStage_mc.getChildByName("MovieMove")) ;
			if (movieMove != null) {				
				isTranfer = false ;
				currTable.ArrPlayers[currPosition].ArrMovie[csMove].visible = true ;
				mainStage_mc.removeChild(movieMove) ;
			}
			//}
			//{ Tao Tieng
			if (isSound) {
				try
				{
					if (removeSound == null) {
						removeSound = new Sound(new URLRequest(preURLSound + "Remove.mp3"));
						removeSound.addEventListener(Event.COMPLETE, PlaySoundRemove) ;
					} else {
						removeSound.play()
					}
				} catch (e:Error) { }
			}
			//}
			//trace("str:" + str) ;
			var objCard : Card = new Card() ; //Con bài thêm vào danh sách
			var object = JSON.decode(str) ;
			var cs : int = 0;
			var name : String = object.Name ;
			var position : Number ;
			var tmpCard : MovieClip ;
			var j : int ;
			var i : int ;
			var k : int ;
			for (j = 0; j < currTable.ArrPlayers.length; j++) 
			{
				if (currTable.ArrPlayers[j].PlayerName == name) {
					cs = j ;
					break ;
				}				
			}	
			if (cs == currPosition) { //Làm hiệu ứng bay con bài nếu ở vị trí hiện tại
				for (j = 0; j < currTable.ArrPlayers[currPosition].ArrCard.length; j++) 
				{				
					if ((currTable.ArrPlayers[currPosition].ArrCard[j].NumberCard == object.N)
						&& (currTable.ArrPlayers[currPosition].ArrCard[j].Suit == object.S)) {
						//Chuyen trang thai dong ho , An dong ho o vi tri hien tai
						currentAction = "Boc" ;	
						currentTimerRun = currPosition + 1;	
						if (currentTimerRun == currTable.CurrentPlayer) currentTimerRun = 0 ;
						mainStage_mc.PlayerBottom_mc.txtTime_mc.text = "" ;
						
						var objCard : Card = new Card() ;
						objCard.NumberCard = currTable.ArrPlayers[currPosition].ArrCard[j].NumberCard;						
						objCard.Suit = currTable.ArrPlayers[currPosition].ArrCard[j].Suit;
						objCard.State = 0 ;
						if (currTable.ArrPlayers[currPosition].ArrCardRemove == null) currTable.ArrPlayers[currPosition].ArrCardRemove = new Array() ;
					
						if (currTable.ArrPlayers[currPosition].ArrCardRemove.length != 0) {
							objCard.X = currTable.ArrPlayers[currPosition].ArrCardRemove[currTable.ArrPlayers[currPosition].ArrCardRemove.length -1].X + 18 ; //positionCards[4].x0 + 20 * (currTable.ArrObjectCards[4].ArrCard.length - 1);
							objCard.Y = currTable.ArrPlayers[currPosition].ArrCardRemove[currTable.ArrPlayers[currPosition].ArrCardRemove.length -1].Y;
						} else {
							objCard.X = positionCards[4].x0 ;
							objCard.Y = positionCards[4].y0 ;
						}
						currTable.ArrPlayers[currPosition].ArrCardRemove.push(objCard) ;//Them mot con bai vao danh sach danh
						if (currTable.ArrPlayers[currPosition].ArrMovieRemove == null) currTable.ArrPlayers[0].ArrMovieRemove = new Array() ;
						//Them Movie vao danh sach Movie da danh
						var position : Number = objCard.NumberCard + (objCard.Suit - 1) * 13 + 1 ;
						var tmpCard : MovieClip = MovieClip(new card()) ;
						tmpCard.x = objCard.X ;
						tmpCard.y = objCard.Y ;
						tmpCard.vien_mc.visible = false ;
						//trace(tmpCard.x + "--->" + tmpCard.y + ":" + position) ;
						tmpCard.gotoAndStop(position);
						currTable.ArrPlayers[currPosition].ArrMovieRemove.push(tmpCard) ;
						//Cap nhat lai toa do cho cac con bai sau no
						for (k = j+1; k < currTable.ArrPlayers[currPosition].ArrCard.length; k++) 
						{
							currTable.ArrPlayers[currPosition].ArrCard[k].X = currTable.ArrPlayers[currPosition].ArrCard[k].X - 20;
						}
						//trace("Length:" + currTable.ArrPlayers[currPosition].ArrMovieRemove.length + "--" + currTable.ArrPlayers[currPosition].ArrMovie.length) ;
						mainStage_mc.removeChild(currTable.ArrPlayers[currPosition].ArrMovie[j]) ; //Remove khoi giao dien		
						currTable.ArrPlayers[currPosition].ArrMovie.splice(j, 1) ; //Remove khoi danh sach Movie					
						currTable.ArrPlayers[currPosition].ArrCard.splice(j, 1) ; //Remove khoi danh sach bai dang cam
						//Hien thi lai cac con bai
						for (i = 0; i < currTable.ArrPlayers[currPosition].ArrCard.length; i++) 
						{
							var item: Card = currTable.ArrPlayers[currPosition].ArrCard[i];
							currTable.ArrPlayers[currPosition].ArrMovie[i].x = currTable.ArrPlayers[currPosition].ArrCard[i].X ;
							currTable.ArrPlayers[currPosition].ArrMovie[i].y = currTable.ArrPlayers[currPosition].ArrCard[i].Y ;	
							var positionM :Number = currTable.ArrPlayers[currPosition].ArrCard[i].NumberCard + (currTable.ArrPlayers[currPosition].ArrCard[i].Suit - 1) * 13 + 1 ;				
							currTable.ArrPlayers[currPosition].ArrMovie[i].gotoAndStop(positionM) ;
						}				
						mainStage_mc.addChild(tmpCard) ;					
						//==> set enable = false voi nut danh
						mainStage_mc.btnDanhbai.gotoAndStop(2) ;
						mainStage_mc.btnDanhbai.removeEventListener(MouseEvent.CLICK, clickRemoveCard) ;
						mainStage_mc.btnHabai.gotoAndStop(2) ;
						mainStage_mc.btnHabai.removeEventListener(MouseEvent.CLICK, clickDownCard) ;
						mainStage_mc.btnGuibai.gotoAndStop(2) ;
						mainStage_mc.btnGuibai.removeEventListener(MouseEvent.CLICK, clickAttachCard) ;
						break ;					
					}
				}
			}
			if (cs != currPosition) {
				//{ Khong phai thang danh
				//Khoi tao lai thoi gian dem
				currentAction = "Boc" ;
				//An dong ho ban o vi tri hien tai
				if ((currPosition - currentTimerRun == 2) || (currPosition - currentTimerRun == -2)) {
					mainStage_mc.PlayerTop_mc.txtTime_mc.text = "" ;				
				} else if ((currPosition - currentTimerRun == 1) || (currPosition - currentTimerRun == -3)) {
					mainStage_mc.PlayerLeft_mc.txtTime_mc.text = "" ;				
				} else if ((currPosition - currentTimerRun == -1) || (currPosition - currentTimerRun == 3)){
					mainStage_mc.PlayerRight_mc.txtTime_mc.text = "" ;				
				}				
				currentTimerRun = cs + 1 ;	
				if (currentTimerRun == currTable.CurrentPlayer) currentTimerRun = 0 ;
				//trace("vitri:" + currPosition + "-" + cs +"-" + currTable.ArrPlayers[cs].ArrCardRemove.length) ;
				objCard.NumberCard = object.N ;
				objCard.Suit = object.S ;
				objCard.State = 0 ;
				if (currTable.ArrPlayers[cs].ArrCardRemove == null) currTable.ArrPlayers[cs].ArrCardRemove = new Array() ;
				if (currTable.ArrPlayers[cs].ArrMovieRemove == null) currTable.ArrPlayers[cs].ArrMovieRemove = new Array() ;
				if (currTable.ArrPlayers[cs].ArrCardRemove.length == 0) {
					if ((currPosition - cs == 2) || (currPosition - cs == -2)) { //Top
						objCard.X = positionCards[6].x0 ;
						objCard.Y = positionCards[6].y0 ;					
					} else if ((currPosition - cs == -3) || (currPosition - cs == 1)) { //Left
						objCard.X = positionCards[7].x0 ;
						objCard.Y = positionCards[7].y0 ;					
					} else if ((currPosition - cs == -1) || (currPosition - cs == 3)) { //Right
						objCard.X = positionCards[5].x0 ;
						objCard.Y = positionCards[5].y0 ;
					}				
				} else {
					//if ((currPosition - cs == -1) || (currPosition - cs == 3)) { 
					//	objCard.X = currTable.ArrPlayers[cs].ArrCardRemove[currTable.ArrPlayers[cs].ArrCardRemove.length - 1].X - 15;
					//	objCard.Y = currTable.ArrPlayers[cs].ArrCardRemove[currTable.ArrPlayers[cs].ArrCardRemove.length - 1].Y ;
					//} else {
						objCard.X = currTable.ArrPlayers[cs].ArrCardRemove[currTable.ArrPlayers[cs].ArrCardRemove.length - 1].X + 18;
						objCard.Y = currTable.ArrPlayers[cs].ArrCardRemove[currTable.ArrPlayers[cs].ArrCardRemove.length - 1].Y ;	
					//}
				}
				//trace("TD:" + objCard.X + "-" + objCard.Y + "-" + objCard.State) ;
				currTable.ArrPlayers[cs].ArrCardRemove.push(objCard) ;
				position = objCard.NumberCard + (objCard.Suit - 1) * 13 + 1 ;
				tmpCard = MovieClip(new card()) ;
				tmpCard.x = objCard.X;
				tmpCard.y = objCard.Y;
				tmpCard.vien_mc.visible = false ;
				tmpCard.gotoAndStop(position);
				mainStage_mc.addChild(tmpCard) ;
				if (!isView)
				if ((cs + 1 == currPosition) || ((cs + 1 == currTable.CurrentPlayer) && (currPosition == 0))) {
					if (currTable.ArrPlayers[currPosition].ArrCardRemove.length < 4) {
						//check
						var tmpAn:MovieClip = MovieClip(new An());
						tmpAn.name = "An";
						if (currPosition - cs == -2) {  //right
							tmpAn.x = cardX[6] + (currTable.ArrPlayers[cs].ArrCardRemove.length - 1) * 18 ;
							tmpAn.y = cardY[6] + 5 ; 
						} else if (currPosition - cs == -1) {   //bottom
							tmpAn.x = cardX[5] + (currTable.ArrPlayers[cs].ArrCardRemove.length - 1) * 18 ;
							tmpAn.y = cardY[5]  + 5  ; 
						} else if ((currPosition - cs == 1) || (currPosition - cs == -3)) { /// left
							tmpAn.x = cardX[7] + (currTable.ArrPlayers[cs].ArrCardRemove.length - 1) * 18 ;
							tmpAn.y = cardY[7] + 5  ; 
							tmpAn.y = cardY[7] + 5  ; 
						}
						mainStage_mc.btnAddCard.visible = true ;					
						mainStage_mc.addChild(tmpAn);					
						tmpAn.addEventListener(MouseEvent.CLICK, clickTakeCard) ;
					}					
				} else 
					mainStage_mc.btnAddCard.visible = false ;
				currTable.ArrPlayers[cs].ArrMovieRemove.push(tmpCard) ;
				currTable.ArrPlayers[cs].ArrCard.shift() ; //Loai bo khoi danh sach con bai cua player danh bai
				mainStage_mc.removeChild(currTable.ArrPlayers[cs].ArrMovie[0]) ; //Loai bo khoi giao dien
				currTable.ArrPlayers[cs].ArrMovie.shift() ; //Loai bo danh sach Movie cua thang danh bai
				//Chinh lai toa do cua cac con bai
				for (k = 0; k < currTable.ArrPlayers[cs].ArrCard.length; k++) 
				{
					if ((currPosition - cs == 2) || (currPosition - cs == -2)) {
						currTable.ArrPlayers[cs].ArrCard[k].X = currTable.ArrPlayers[cs].ArrCard[k].X - 20 ;
						currTable.ArrPlayers[cs].ArrMovie[k].x = currTable.ArrPlayers[cs].ArrMovie[k].x - 20 ;
					} else {
						currTable.ArrPlayers[cs].ArrCard[k].Y = currTable.ArrPlayers[cs].ArrCard[k].Y - 20 ;
						currTable.ArrPlayers[cs].ArrMovie[k].y = currTable.ArrPlayers[cs].ArrMovie[k].y - 20 ;
					}				
				}
				//}
			} else if (isView == true) { 
				//{Trong truong hop View
				//Khoi tao lai thoi gian dem
				currentAction = "Boc" ;
				//An dong ho ban o vi tri hien tai
				if ((currPosition - currentTimerRun == 2) || (currPosition - currentTimerRun == -2)) {
					mainStage_mc.PlayerTop_mc.txtTime_mc.text = "" ;				
				} else if ((currPosition - currentTimerRun == 1) || (currPosition - currentTimerRun == -3)) {
					mainStage_mc.PlayerLeft_mc.txtTime_mc.text = "" ;				
				} else if ((currPosition - currentTimerRun == -1) || (currPosition - currentTimerRun == 3)){
					mainStage_mc.PlayerRight_mc.txtTime_mc.text = "" ;
				} else 
					mainStage_mc.PlayerBottom_mc.txtTime_mc.text = "" ;	
				currentTimerRun = cs + 1 ;	
				if (currentTimerRun == currTable.CurrentPlayer) currentTimerRun = 0 ;
				
				objCard.NumberCard = object.N ;
				objCard.Suit = object.S ;
				objCard.State = 0 ;
				if (currTable.ArrPlayers[cs].ArrCardRemove == null) {
					currTable.ArrPlayers[cs].ArrCardRemove = new Array() ;
					objCard.X = cardX[4] ;
				} else
					objCard.X = cardX[4] + currTable.ArrPlayers[cs].ArrCardRemove.length * 15;
				if (currTable.ArrPlayers[cs].ArrMovieRemove == null) currTable.ArrPlayers[cs].ArrMovieRemove = new Array() ;				
				objCard.Y = cardY[4] ;
				currTable.ArrPlayers[cs].ArrCardRemove.push(objCard) ;
				position = objCard.NumberCard + (objCard.Suit - 1) * 13 + 1 ;
				tmpCard = MovieClip(new card()) ;
				tmpCard.x = objCard.X;
				tmpCard.y = objCard.Y;
				tmpCard.vien_mc.visible = false ;
				tmpCard.gotoAndStop(position);
				mainStage_mc.addChild(tmpCard) ;
				currTable.ArrPlayers[cs].ArrMovieRemove.push(tmpCard) ;				
				mainStage_mc.removeChild(currTable.ArrPlayers[currPosition].ArrMovie.shift()) ; //Remove khoi giao dien		
				currTable.ArrPlayers[currPosition].ArrCard.shift() ; //Remove khoi danh sach bai dang cam
				//}
			}
			//Neu tat ca nguoi choi deu da ha thi ko khoi tao thoi gian nua
			for (i= 0; i < currTable.ArrPlayers.length; i++) 
			{
				if (currTable.ArrPlayers[i].ArrCardRemove.length < 4) {
					//Khoi tao lai thoi gian boc
					if (mainTimer.running)
						mainTimer.stop() ;
					mainTimer = new Timer(1000) ;
					mainTimer.repeatCount = 10 ;					
					mainTimer.addEventListener(TimerEvent.TIMER, StartTimeMain);				
					mainTimer.start() ;	
					return ;
				}
			}
			if (mainTimer.running)
				mainTimer.stop() ;			
		}
		/************************************** An Bai ********************************************************/
		/*
		 * Player an bai
		 * ==> Dieu khien hien thi giao dien
		 * ==> Gui thong tin con bai an cho Server
		 */ 
		public static function clickTakeCard(evt : MouseEvent) {
			TakeCard() ;			
		}
		public static function TakeCard() {
			var cs : int = currPosition - 1 ;
			if (cs < 0) cs = currTable.CurrentPlayer - 1 ;
			var objCard : Card = currTable.ArrPlayers[cs].ArrCardRemove.pop() ; // new Card() ; //Con bài thêm vào danh sách
			objCard.State = 2 ; //Con bai an
			objCard.X = currTable.ArrPlayers[currPosition].ArrCard[currTable.ArrPlayers[currPosition].ArrCard.length - 1].X + 20 ;
			objCard.Y = currTable.ArrPlayers[currPosition].ArrCard[currTable.ArrPlayers[currPosition].ArrCard.length - 1].Y ;
			//Dem so con da an
			var demTake : int = 0;
			for (var j:int = 0; j < currTable.ArrPlayers[currPosition].ArrCard.length; j++) 
			{
				if (currTable.ArrPlayers[currPosition].ArrCard[j].State == 2) demTake++ ;				
			}
			//Them Movie
			var tmpCard : MovieClip = currTable.ArrPlayers[cs].ArrMovieRemove.pop() ;//MovieClip(new card()) ;
			tmpCard.vien_mc.visible = true ;
			//trace("==>" + objCard.X + "-" + objCard.Y) ;
			moveCard(tmpCard, objCard.X, objCard.Y) ; //Tao hieu ung di chuyen con bai
			mainStage_mc.removeChild(tmpCard) ;
			//Hien thi lai tren giao dien
			var _mc : MovieClip = MovieClip(new card()) ;
			_mc.x = objCard.X ;
			_mc.y = objCard.Y ;
			_mc.vien_mc.visible = true ;			
			_mc.gotoAndStop(objCard.NumberCard + (objCard.Suit - 1) * 13 + 1) ;
			new CardController(_mc, objCard) ; //Khoi tao de dieu khien
			//trace("==>" + _mc.x + "-" + _mc.y) ;			
			mainStage_mc.addChild(_mc) ;
			currTable.ArrPlayers[currPosition].ArrCard.push(objCard) ;
			currTable.ArrPlayers[currPosition].ArrMovie.push(_mc) ;
			//Remove button An
			var tmpAn:MovieClip = MovieClip(mainStage_mc.getChildByName("An"));
			mainStage_mc.removeChild(tmpAn) ;
			mainStage_mc.btnAddCard.visible = false ;
			var strSend : String = "AC:" + currTable.TableID + "CR:" + objCard.Suit + objCard.NumberCard + "ID:" + user.PlayId ;
			mainSocket.sendData(strSend) ;			
			//Kiem tra neu an 3 cay
			demTake ++ ;			
			//Check xem co u luon duoc khong ?
			//if (demTake < 3) {
				//SortForNumber() ;
				ArrState = new Array() ;
				playerCards = null ;
				playerCards = new Array() ;
				//Khoi tao cho mang ArrState 0 - 0 vao phom, 1 vao phom doc, 2 vao phom ngang - 3 vao ca phom doc va phom ngang
				for (var i:int = 0; i < currTable.ArrPlayers[currPosition].ArrCard.length; i++)
					playerCards.push(currTable.ArrPlayers[currPosition].ArrCard[i]) ;
				playerCards.sortOn(["NumberCard", "Suit"], [Array.NUMERIC, Array.NUMERIC]);
				for (var k:int = 0; k < playerCards.length; k++) 
				{				
					var isNgang : Boolean = IsNumberCard(k) ;
					var isDoc : Boolean = IsSuit(k) ;
					//trace("ABC:" + currTable.ArrPlayers[currPosition].ArrCard[i].NumberCard + currTable.ArrPlayers[currPosition].ArrCard[i].Suit + "-" + isNgang + "-" + isDoc);
					if (isNgang) {
						if (isDoc) 
							ArrState.push("3") ;						
						else 
							ArrState.push("2") ;					
					} else if (isDoc) 
						ArrState.push("1") ;					
					else 
						ArrState.push("0") ;					
				}
				CountSelection = 0 ;
				MinCard = 10 ;
				//Goi ham De Quy			
				Recursive(0) ;
				//trace("Check U:" + MinCard) ;
				if (MinCard < 2) {
					SortForNumber() ;
					if (currTable.ArrPlayers[currPosition].ArrCard.length < 9)
						AutoDownCard("HC") ;
					else
						AutoDownCard("HU") ;
					//AutoDownCard("HU") ;
					isU = true ;
				}
			//}
			//An cac nut boc va enable=true nut danh
			mainStage_mc.btnDanhbai.addEventListener(MouseEvent.CLICK, clickRemoveCard) ;
			mainStage_mc.btnDanhbai.gotoAndStop(1) ;
			if ((demTake == 3) && (!isU)) {
				mainSocket.sendData("DL:" + currTable.TableID + "ID:" + user.PlayId) ;				
			}
		}
		/*
		 * Nhan duoc thong tin tu Server Player khac an bai
		 */ 
		public static function PlaySoundTake(event : Event) {
			takeSound.play() ;
		}
		public static function OtherTakeCard(str : String) {
			tf.appendText("\n Take card: " + str);
			//{ Tao Tieng
			if (isSound) {
				try
				{
					if (takeSound == null) {
						takeSound = new Sound(new URLRequest(preURLSound + "Take.mp3"));
						takeSound.addEventListener(Event.COMPLETE, PlaySoundTake) ;
					} else {
						takeSound.play()
					}
				} catch (e:Error) { }
			}
			//}
			var formatText : TextFormat = new TextFormat() ;				
			formatText.font = "Tahoma" ;
			formatText.bold = true ;
			formatText.size = 11 ;
			//Khoi dong thoi gian hien thi diem tru
			if (TakeTimer.running) {
				TakeTimer.stop() ;
				var _mc1 : MovieClip = MovieClip(mainStage_mc.getChildByName("MarkDiscount")) ;
				if (_mc1 != null)
					mainStage_mc.removeChild(_mc1) ;
				if ((currPosition - TakeCS == -2) || (currPosition - TakeCS == 2)) {
					mainStage_mc.PlayerTop_mc.Mark_txt.text = numberFormat(currTable.ArrPlayers[TakeCS].Mark, 0, false, true) + " AG" ;
					mainStage_mc.PlayerTop_mc.Mark_txt.setTextFormat(formatText) ;
				} else if ((currPosition - TakeCS == -1) || (currPosition - TakeCS == 3)) {
					mainStage_mc.PlayerRight_mc.Mark_txt.text = numberFormat(currTable.ArrPlayers[TakeCS].Mark, 0, false, true) + " AG" ;
					mainStage_mc.PlayerRight_mc.Mark_txt.setTextFormat(formatText) ;
				} else if ((currPosition - TakeCS == 1) || (currPosition - TakeCS == -3)) {
					mainStage_mc.PlayerLeft_mc.Mark_txt.text = numberFormat(currTable.ArrPlayers[TakeCS].Mark, 0, false, true) + " AG" ;
					mainStage_mc.PlayerLeft_mc.Mark_txt.setTextFormat(formatText) ;
				} else 
					mainStage_mc.PlayerBottom_mc.Mark_txt.text = numberFormat(currTable.ArrPlayers[TakeCS].Mark, 0, false, true) + " AG" ;
			}
			//trace("str:" + str) ;
			//tf.appendText("AC:" + str + "\n") ;
			var objCard : Card = new Card() ; //Con bài thêm vào danh sách
			var object = JSON.decode(str) ;
			var cs : int = 0;
			var name : String = object.Name ; //Ten Player an
			for (var j:int = 0; j < currTable.ArrPlayers.length; j++) 
			{
				if (currTable.ArrPlayers[j].PlayerName == name) {
					cs = j ;
					break ;
				}
			}
			//Dem so cay an
			var demTake : int = 0 ;
			for (var i:int = 0; i < currTable.ArrPlayers[cs].ArrCard.length; i++) 
			{
				if (currTable.ArrPlayers[cs].ArrCard[i].State == 2) demTake++ ;				
			}
			var preCs : int = cs - 1 ;
			if (preCs < 0) preCs = currTable.CurrentPlayer - 1 ;
			//trace("vitri:" + currPosition + "-" + cs +"-" + currTable.ArrPlayers[preCs].ArrCardRemove.length + "-" + preCs) ;
			//Hien thi lai thong tin diem
			if (currTable.ArrPlayers[cs].ArrCardRemove == null) currTable.ArrPlayers[cs].ArrCardRemove = new Array() ;			
			var markDiscount : Number = 0 ;
			//tf.appendText("cs:" + cs + "-" + currPosition) ;
			
			
			if (cs == currPosition) {
				if (currTable.ArrPlayers[currPosition].ArrCardDow != null)
					for (var k:int = 0; k < currTable.ArrPlayers[currPosition].ArrCardDow.length; k++) 
					{
						for (var l:int = 0; l < currTable.ArrPlayers[currPosition].ArrCardDow[k].length; l++) 
						{
							if (currTable.ArrPlayers[currPosition].ArrCardDow[k][l].State == 2) demTake++ ;							
						}						
					}
				//tf.appendText("take:" + demTake) ;
				if (currPlayer.Type == 0) demTake++;
				if (currTable.ArrPlayers[cs].ArrCardRemove.length > 2)
					{markDiscount = currTable.MarkUnit * 4 ;    // cay chot ha
					ChotName = object.Name;}
				else 
					markDiscount = currTable.MarkUnit * demTake ; //(currTable.ArrPlayers[preCs].ArrCardRemove.length + 1) ;
			} else {
				if (currTable.ArrPlayers[cs].ArrCardRemove.length > 2)
					{markDiscount = currTable.MarkUnit * 4 ;
					ChotName = object.Name;}
				else markDiscount = currTable.MarkUnit * (demTake + 1) ; // currTable.ArrPlayers[preCs].ArrCardRemove.length ;
			}
			//tf.appendText("take:" + demTake + "-" + markDiscount) ;
			if (markDiscount > currTable.ArrPlayers[preCs].Mark)
				markDiscount = currTable.ArrPlayers[preCs].Mark ;
			currTable.ArrPlayers[preCs].Mark = currTable.ArrPlayers[preCs].Mark - markDiscount ;
			currTable.ArrPlayers[cs].Mark = currTable.ArrPlayers[cs].Mark + markDiscount * 95/100;
			currTable.ArrPlayers[cs].MarkTakeInGame = currTable.ArrPlayers[cs].MarkTakeInGame + markDiscount * 95/100 ;			
			var _mcDiscount : MovieClip = MovieClip(new MarkDiscount()) ;
			_mcDiscount.name = "MarkDiscount" ;
			_mcDiscount.MarkDiscount_txt.text = markDiscount ; //Hien thi thong bao tru diem
			if (markDiscount < 1000)
				_mcDiscount.IconXu.x = 28 ;
			else if (markDiscount < 10000)
				_mcDiscount.IconXu.x = 16 ;
			else _mcDiscount.IconXu.x = 0 ;
			var xNew : Number = 0 ;
			var yNew : Number = 0 ;
			var positionKhung : int = 1 ;
			if (currTable.ArrPlayers[preCs].Mark > vipMark) {
				formatText.color = 0x000000 ;
				positionKhung = 2 ;
			} else 
				formatText.color = 0xFFFFFF ;
			if ((currPosition - preCs == -2) || (currPosition - preCs == 2)) {
				mainStage_mc.PlayerTop_mc.Mark_txt.text = numberFormat(currTable.ArrPlayers[preCs].Mark, 0, false, true) + " AG";
				mainStage_mc.PlayerTop_mc.Mark_txt.setTextFormat(formatText) ;
				mainStage_mc.PlayerTop_mc.Khung_mc.gotoAndStop(positionKhung) ;
				_mcDiscount.x = 160 ;
				_mcDiscount.y = 50 ;
			} else if ((currPosition - preCs == -1) || (currPosition - preCs == 3)) {
				mainStage_mc.PlayerRight_mc.Mark_txt.text = numberFormat(currTable.ArrPlayers[preCs].Mark, 0, false, true) + " AG";
				mainStage_mc.PlayerRight_mc.Mark_txt.setTextFormat(formatText) ;
				mainStage_mc.PlayerRight_mc.Khung_mc.gotoAndStop(positionKhung) ;
				_mcDiscount.x = 680 ;
				_mcDiscount.y = 300 ;
			} else if ((currPosition - preCs == 1) || (currPosition - preCs == -3)) {
				mainStage_mc.PlayerLeft_mc.Mark_txt.text = numberFormat(currTable.ArrPlayers[preCs].Mark, 0, false, true) + " AG";
				mainStage_mc.PlayerLeft_mc.Mark_txt.setTextFormat(formatText) ;
				mainStage_mc.PlayerLeft_mc.Khung_mc.gotoAndStop(positionKhung) ;
				_mcDiscount.x = -8 ;
				_mcDiscount.y = 300 ;
			} else {
				mainStage_mc.PlayerBottom_mc.Mark_txt.text = numberFormat(currTable.ArrPlayers[preCs].Mark, 0, false, true) + " AG";
				mainStage_mc.PlayerBottom_mc.Mark_txt.setTextFormat(formatText) ;
				mainStage_mc.PlayerBottom_mc.Khung_mc.gotoAndStop(positionKhung) ;
				_mcDiscount.x = 520 ;
				_mcDiscount.y = 500 ;
			}
		

			//Hien thi cac con bai
			var _mc : MovieClip ;
			if ((cs != currPosition) || (isView)) {
				objCard.NumberCard = object.N ;
				objCard.Suit = object.S ;
				objCard.State = 2 ; //Con bai an			
				if ((currPosition - cs == 2) || (currPosition - cs == -2) || (currPosition == cs)) {
					objCard.X = currTable.ArrPlayers[cs].ArrCard[currTable.ArrPlayers[cs].ArrCard.length - 1].X + 20 ;
					objCard.Y = currTable.ArrPlayers[cs].ArrCard[currTable.ArrPlayers[cs].ArrCard.length - 1].Y ;
				} else {
					objCard.X = currTable.ArrPlayers[cs].ArrCard[currTable.ArrPlayers[cs].ArrCard.length - 1].X ;
					objCard.Y = currTable.ArrPlayers[cs].ArrCard[currTable.ArrPlayers[cs].ArrCard.length - 1].Y + 28;
				}
				currTable.ArrPlayers[cs].ArrCard.push(objCard) //Them vao danh sach cua Player an
				//Tao Movie				
				var tmpCard : MovieClip = currTable.ArrPlayers[preCs].ArrMovieRemove.pop() ; //MovieClip(new card()) ;
				moveCard(tmpCard, objCard.X, objCard.Y) ;
				if (cs != currPosition)
					_mc = MovieClip(new card()) ;
				else 
					_mc = MovieClip(new card()) ;
				_mc.x = objCard.X ;
				_mc.y = objCard.Y ;
				//trace("==>" + _mc.x + "-" + _mc.y) ;
				_mc.vien_mc.visible = true ;
				//new CardController(_mc, objCard) ; //Khoi dong de dieu khien con bai
				_mc.gotoAndStop(objCard.NumberCard + (objCard.Suit - 1) * 13 + 1) ;
				mainStage_mc.removeChild(tmpCard) ;
				mainStage_mc.addChild(_mc) ;
				currTable.ArrPlayers[cs].ArrMovie.push(_mc) ; //Them Movie cua con bai an
				//Remove khoi danh sach con bai danh cua cs-1				
				currTable.ArrPlayers[preCs].ArrCardRemove.splice(currTable.ArrPlayers[preCs].ArrCardRemove.length - 1) ;			
			}
			
			
	// Dieu khien so tien chay sang thang an
				mainStage_mc.addChild(_mcDiscount) ;
			if ((currPosition - cs == -2) || (currPosition - cs == 2)) {
				xNew = 160 ;
				yNew = 50 ;				
			} else if ((currPosition - cs == -1) || (currPosition - cs == 3)) {
				xNew = 680 ;
				yNew = 300 ;
			} else if ((currPosition - cs == 1) || (currPosition - cs == -3)) {
				xNew = -8 ;
				yNew = 300 ;
			} else {
				xNew = 520 ;
				yNew = 500 ;
			}
			moveMarkDiscount(_mcDiscount, xNew, yNew) ;
			TakeCS = cs ;			
			TakeTimer = new Timer(1000) ;
			TakeTimer.repeatCount = 6 ;
			TakeTimer.addEventListener(TimerEvent.TIMER, StartTimeTake);				
			TakeTimer.start() ;
			
			
			
			//Tim kiem de chuyen phom
			if (currTable.ArrPlayers[cs].ArrCardRemove.length >= currTable.ArrPlayers[preCs].ArrCardRemove.length)
			{
				var t : Boolean = true ;				
				while (t) {
					t = true ;
					//trace("Cs-PreCS:" + cs + "-" + preCs) ;
					if (cs == preCs) break ;
					//trace("Length:" + currTable.ArrPlayers[cs].ArrCardRemove.length + "-" + currTable.ArrPlayers[preCs].ArrCardRemove.length) ;
					if (currTable.ArrPlayers[cs].ArrCardRemove.length > currTable.ArrPlayers[preCs].ArrCardRemove.length) {
						t = false ;
						//Chuyen con bai cuoi cung da danh cua cs ==> preCs
						var mcTemp : MovieClip = currTable.ArrPlayers[cs].ArrMovieRemove.pop() ;
						var objTemp : Card = currTable.ArrPlayers[cs].ArrCardRemove.pop() ;
						if (currTable.ArrPlayers[preCs].ArrCardRemove.length == 0) {
							if ((currPosition - preCs == 2) || (currPosition - preCs == -2)) {
								objTemp.X = positionCards[6].x0 ;
								objTemp.Y = positionCards[6].y0 ;
							} else if ((currPosition - preCs == -1) || (currPosition - preCs == 3)) {
								objTemp.X = positionCards[5].x0 ;
								objTemp.Y = positionCards[5].y0 ;
							} else if ((currPosition - preCs == 1) || (currPosition - preCs == -3)) {
								objTemp.X = positionCards[7].x0 ;
								objTemp.Y = positionCards[7].y0 ;
							} else {
								objTemp.X = positionCards[4].x0 ;
								objTemp.Y = positionCards[4].y0 ;
							}
						} else {
							//if ((currPosition - preCs == 3) || (currPosition - preCs == -1)) {
								//objTemp.X = currTable.ArrPlayers[preCs].ArrCardRemove[currTable.ArrPlayers[preCs].ArrCardRemove.length - 1].X - 15 ;
								//objTemp.Y = currTable.ArrPlayers[preCs].ArrCardRemove[currTable.ArrPlayers[preCs].ArrCardRemove.length - 1].Y ;
							//} else {
								objTemp.X = currTable.ArrPlayers[preCs].ArrCardRemove[currTable.ArrPlayers[preCs].ArrCardRemove.length - 1].X + 15 ;
								objTemp.Y = currTable.ArrPlayers[preCs].ArrCardRemove[currTable.ArrPlayers[preCs].ArrCardRemove.length - 1].Y ;
							//}
						}
						//trace("TDN:" + objTemp.X + "-" + objTemp.Y) ;
						moveCard(mcTemp, objTemp.X, objTemp.Y) ;
						_mc = MovieClip(new card()) ;
						_mc.x = objTemp.X ; 
						_mc.y = objTemp.Y ; 
						_mc.vien_mc.visible = false ;
						_mc.gotoAndStop(objTemp.NumberCard + (objTemp.Suit - 1) * 13 + 1) ;
						mainStage_mc.removeChild(mcTemp) ;
						mainStage_mc.addChild(_mc) ;
						currTable.ArrPlayers[preCs].ArrCardRemove.push(objTemp) ;
						currTable.ArrPlayers[preCs].ArrMovieRemove.push(_mc) ;
					}
					cs = cs + 1 ;
					if (cs == currTable.CurrentPlayer) cs = 0 ;					
				}
			}	
			if (isU) return ;
			if (mainTimer.running)
				mainTimer.stop() ;
			mainTimer = new Timer(1000) ;			
			if ((currTable.ArrPlayers[currPosition].ArrCardRemove.length == 3) && (currPosition == currentTimerRun) && (isView == false)) {
				//Hien thi popup
				//mainStage_mc.bgMain_mc.visible = true;
				//Hide
				var tmpHide:MovieClip = MovieClip(new Hide());
				tmpHide.name = "Hide";
				tmpHide.x = 0 ;
				tmpHide.y = 0 ;
				tmpHide.alpha = 0 ;
				mainStage_mc.addChild(tmpHide) ;
				var tmpTable:MovieClip = MovieClip(new ChonHa_Popup());
				if (ArrPopupBG[2] != null)
					tmpTable.BG_mc.addChild(ArrPopupBG[2]) ;
				tmpTable.BG_mc.width = 210 ;
				tmpTable.BG_mc.height = 120 ;
				tmpTable.name = "ChonHa_Popup";
				tmpTable.x = 280;
				tmpTable.y = 240;
				tmpTable.btnOK_mc.addEventListener(MouseEvent.CLICK, btnOK_mc_ChonHa_click);
				mainStage_mc.addChild(tmpTable);
				mainStage_mc.btnGuibai.addEventListener(MouseEvent.CLICK, clickAttachCard) ;
				mainStage_mc.btnGuibai.gotoAndStop(1) ;	
				mainTimer.repeatCount = 5 ;
				currentAction = "AutoHa" ;
			} else if (currTable.ArrPlayers[currentTimerRun].ArrCardRemove.length == 3){		
				mainTimer.repeatCount = 35 ;
				currentAction = "Danh" ;
			} else {
				mainTimer.repeatCount = 20 ;				
				currentAction = "Danh" ;
			}
			mainTimer.addEventListener(TimerEvent.TIMER, StartTimeMain);				
			mainTimer.start() ;
		}
		public static function moveMarkDiscount(_mc: MovieClip, xNew:Number, yNew:Number){				
			TweenMax.to(_mc, 10, { x:xNew, y:yNew, ease:Expo.easeOut} );			
		}
		/**************************************** Ha bai ***********************************************/
		public static function btnOK_mc_ChonHa_click(evt : MouseEvent) {
			ChonHa_click() ;
		}	
		public static function ChonHa_click() {
			//{ Tao Tieng
			SoundKick()
			//}
			var tmp:MovieClip = MovieClip(mainStage_mc.getChildByName("ChonHa_Popup")) ;
			var rbg : Object = tmp.rb1.group ;
			if (rbg.selection == null) return ;
			var strValue = rbg.selection.value ;
			if (mainTimer.running)
				mainTimer.stop() ;
			mainTimer = new Timer(1000) ;
			mainStage_mc.removeChild(tmp) ;
			if (strValue == "2") {
				mainStage_mc.btnHabai.addEventListener(MouseEvent.CLICK, clickDownCard) ;
				mainStage_mc.btnHabai.gotoAndStop(1) ;
				mainTimer.repeatCount = 30 ;				
			} else {
				mainTimer.repeatCount = 25 ;				
				AutoFinished() ;				
			}
			//mainStage_mc.bgMain_mc.visible = false ;
			var tmpHide:MovieClip = MovieClip(mainStage_mc.getChildByName("Hide"));			
			while (tmpHide != null){
				mainStage_mc.removeChild(tmpHide) ;
				tmpHide = MovieClip(mainStage_mc.getChildByName("Hide"));			
			}
			currentAction = "Danh" ;
			mainTimer.addEventListener(TimerEvent.TIMER, StartTimeMain);				
			mainTimer.start() ;			
		}
		/******************************************* Finish Game *********************************************/
		public static function PlaySoundFinished(event : Event) {
			finishedSound.play() ;
		}
		public static function FinishedGame(str : String) {		
			//{ Tao Tieng
			if (isSound) {
				try 
				{
					if (finishedSound == null) {
						finishedSound = new Sound(new URLRequest(preURLSound + "Finished.mp3"));
						finishedSound.addEventListener(Event.COMPLETE, PlaySoundFinished) ;
					} else {
						finishedSound.play()
					}
				} catch (e :Error) { }
			}
			//}
			//tf.appendText("Finish:" + str + "-" + currPosition) ;			
			mainTimer.stop() ;
			//Chuyen trang thai cua cac Player			
			for (var t:int = 0; t < currTable.ArrPlayers.length; t++) 
				currTable.ArrPlayers[t].Ready = false ;				
			//Remove het cac bieu tuong An va Boc neu co
			var tmpAn:MovieClip = MovieClip(mainStage_mc.getChildByName("An"));
			if (tmpAn != null)
				mainStage_mc.removeChild(tmpAn) ;
			mainStage_mc.Card00.visible = false ;	
			mainStage_mc.btnAddCard.visible = false ;
			currTable.Status = 2 ;
			//Hide
			var tmpHide:MovieClip = MovieClip(new Hide());
			tmpHide.name = "Hide";
			tmpHide.x = 0 ;
			tmpHide.y = 0 ;
			tmpHide.alpha = 0 ;
			mainStage_mc.addChild(tmpHide) ;
			var tmpResult:MovieClip = MovieClip(new Result_Popup());
			if (ArrPopupBG[3] != null)
				tmpResult.BG_mc.addChild(ArrPopupBG[3]) ;
			tmpResult.name = "Result_Popup";
			tmpResult.x = 230;
			tmpResult.y = 175;				
			tmpResult.btnXacnhan_mc.addEventListener(MouseEvent.CLICK, btnXacnhan_mc_click);
			var pos1 : Boolean = false ;
			var pos2 : Boolean = false ;
			var pos3 : Boolean = false ;
			var pos4 : Boolean = false ;
			var object : Array = (JSON.decode(str) as Array) ;
			//tf.appendText("\n str = " + str);
			
			var totalMoney : Number = 0 ;
			var markPlayer : Number = 0 ;
			var s,u,v : int ;
			//{Hien thi bieu tuong u va den neu co
			for (s = 0; s < object.length; s++) 
			{				
				if ((object[s].Vitri == 6) || (object[s].Vitri == 7)) { //U
					isU = true ;
					var tmpU:MovieClip = MovieClip(new IconU());
					tmpU.name = "IconU";
					tmpU.x = 644 ;
					tmpU.y = 470 ;
					for (u = 0; u < object.length; u++) 
					{
						if (object[u].Vitri == 1) {
							UName = object[u].Name;
							//tf.appendText(u.toString());
							//Tim vi tri cua thang nhat
							for (v = 0; v < currTable.ArrPlayers.length; v++) 
							{
								if (currTable.ArrPlayers[v].PlayerName == object[u].Name) {
									if (currPosition - v == 0) {   //bottom
										tmpU.x = 550 ;
										tmpU.y = 450 ;
									} else if ((currPosition - v == 1) || (currPosition - v == -3)){   //left
										tmpU.x = 20 ;
										tmpU.y = 260 ;
									} else if ((currPosition - v == 2) || (currPosition - v == -2)) {  //top
										tmpU.x = 195 ;
										tmpU.y = 15;
									} else if ((currPosition - v == 3) || (currPosition - v == -1)){	//right							
										tmpU.x = 705 ;
										tmpU.y = 270 ;
									}
									break ;
								}								
							}
							break ;
						}						
					}					
					mainStage_mc.addChild(tmpU) ;
					//UName = object[s].Name;
					//tf.appendText("\n name U = "+UName);
					break ;
				}
			}
			//}
			for (s = 0; s < object.length; s++)
			{
				if ((object[s].Vitri == 9) || (object[s].Vitri == 7)) { //Den
					//isU = true ;
					var tmpDen:MovieClip = MovieClip(new IconDen());
					tmpDen.name = "IconDen";	
					tmpDen.x = 644 ;
					tmpDen.y = 470 ;
					//Tim vi tri cua thang nhat
					for (v = 0; v < currTable.ArrPlayers.length; v++) 
					{
						if (currTable.ArrPlayers[v].PlayerName == object[s].Name) {
							if (currPosition - v == 0) {  //bottom
								tmpDen.x = 575 ;
								tmpDen.y = 510 ;
							} else if ((currPosition - v == 1) || (currPosition - v == -3)){
								tmpDen.x = 55 ;
								tmpDen.y = 315 ;
							} else if ((currPosition - v == 2) || (currPosition - v == -2)) {
								tmpDen.x = 215 ;
								tmpDen.y = 70 ;
							} else if ((currPosition - v == 3) || (currPosition - v == -1)){
								tmpDen.x = 740 ;
								tmpDen.y = 315 ;
							}
							break ;
						}								
					}					
					mainStage_mc.addChild(tmpDen) ;
					break ;
				}
			}
			//tf.appendText("Length:" + object.length + "\n") ;
			//{ Neu la Den lang an lao thi tra lai so tien cua thang bi an lao
			//Tim thang 9 va thang truoc no
			for (var q:int = 0; q < object.length; q++) 
			{
				if (object[q].Vitri == 9) {
					for (var r:int = 0; r < currTable.ArrPlayers.length; r++) 
					{
						if (currTable.ArrPlayers[r].PlayerName == object[q].Name) {
							var csSau : int = r - 1 ;
							if (csSau < 0) csSau =  currTable.CurrentPlayer - 1 ;
							if (currTable.ArrPlayers[r].Mark > currTable.ArrPlayers[r].MarkTakeInGame) {
								currTable.ArrPlayers[csSau].Mark = currTable.ArrPlayers[csSau].Mark + currTable.ArrPlayers[r].MarkTakeInGame ;
								currTable.ArrPlayers[r].Mark = currTable.ArrPlayers[r].Mark - currTable.ArrPlayers[r].MarkTakeInGame ;
							} else {
								currTable.ArrPlayers[csSau].Mark = currTable.ArrPlayers[csSau].Mark + currTable.ArrPlayers[r].Mark ;
								currTable.ArrPlayers[r].Mark = 0 ;
							}
							break ;
						}									
					}								
					break ;
				}							
			}			
			//}
			for (var i:int = 0; i < object.length; i++) 
			{
				var item = object[i];
				var cs : int = 0 ;
				for (var j:int = 0; j < currTable.ArrPlayers.length; j++) 
				{
					if (currTable.ArrPlayers[j].PlayerName == item.Name) {
						//currTable.ArrPlayers[j].Mark = item.Money ;
						cs = j;
						break ;
					}			
				}
				currTable.ArrPlayers[cs].MarkInGame = item.Mark; //Diem trong van choi
				currTable.ArrPlayers[cs].FinishPosition = item.Vitri ;//So thu tu trong van bai
				if (cs != currPosition)					
					for (var k:int = 0; k < item.ArrCard.length; k++) //Hien thi cac con bai con lai
					{
						var objC : Card = new Card() ;
						objC.NumberCard = item.ArrCard[k].N ;
						objC.Suit = item.ArrCard[k].S ;
						objC.State = 0 ;
						if (currTable.ArrPlayers[cs].ArrCard.length > k) {
							currTable.ArrPlayers[cs].ArrCard.NumberCard = item.ArrCard[k].N ;
							currTable.ArrPlayers[cs].ArrCard.Suit = item.ArrCard[k].S ;
							currTable.ArrPlayers[cs].ArrMovie[k].gotoAndStop(objC.NumberCard + (objC.Suit - 1) * 13 + 1) ;
							if ((currPosition - cs == 2) || (currPosition - cs == -2)) {
								currTable.ArrPlayers[cs].ArrMovie[k].x = 376 - (item.ArrCard.length * 15) / 2  + (k -1) * 18 ;
							} else {
								currTable.ArrPlayers[cs].ArrMovie[k].y = 280 - (item.ArrCard.length * 20)/ 2  + (k -1) * 28 ;
							}
						}
					}				
				if (currTable.ArrPlayers[cs].FinishPosition == 0) {
					if (!pos2) {
						tmpResult.Player2_mc.Name_txt.text = currTable.ArrPlayers[cs].PlayerName ;
						tmpResult.Player2_mc.Mark_txt.text = currTable.ArrPlayers[cs].MarkInGame ;
						tmpResult.Player2_mc.Money_txt.text = "0" ;
						pos2 = true ;
					} else if (!pos3) {
						tmpResult.Player3_mc.Name_txt.text = currTable.ArrPlayers[cs].PlayerName ;
						tmpResult.Player3_mc.Mark_txt.text = currTable.ArrPlayers[cs].MarkInGame ;
						tmpResult.Player3_mc.Money_txt.text = "0" ;
						pos3 = true ;
					}
				} else if (currTable.ArrPlayers[cs].FinishPosition == 1) {
					tmpResult.Player1_mc.Name_txt.text = currTable.ArrPlayers[cs].PlayerName ;
					tmpResult.Player1_mc.Mark_txt.text = currTable.ArrPlayers[cs].MarkInGame ;
					totalMoney = 0 ;
					for (var l:int = 0; l < object.length; l++) 
					{
						if (object[l].Vitri == 1) continue ;
						markPlayer = 0 ;
						for (var n:int = 0; n < currTable.ArrPlayers.length; n++) 
						{
							if (currTable.ArrPlayers[n].PlayerName == object[l].Name) {
								markPlayer = currTable.ArrPlayers[n].Mark ;
								break ;
							}							
						}
						//tf.appendText("1==>Tien:" + markPlayer + "\n") ;
						if (object[l].Vitri == 7) {
							if (currTable.MarkUnit * 15 > markPlayer)
								totalMoney = totalMoney + markPlayer ;
							else
								totalMoney = totalMoney + currTable.MarkUnit * 5 * (currTable.CurrentPlayer-1) ;							
						} else if (object[l].Vitri != 0){
							if (currTable.MarkUnit * (object[l].Vitri - 1) > markPlayer)
								totalMoney = totalMoney + markPlayer ;
							else
								totalMoney = totalMoney + currTable.MarkUnit * (object[l].Vitri - 1) ;
								
						}											
					}
					tmpResult.Player1_mc.Money_txt.text = numberFormat((totalMoney * 95) /100, 0, false, true) ;
					currTable.ArrPlayers[cs].Mark = currTable.ArrPlayers[cs].Mark + (totalMoney * 95) /100 ;
					pos1 = true ;
					//Hien thi bieu tuong
					var tmpNhat : MovieClip = MovieClip(new Nhat()) ;
					if(isU)
					{
						tmpNhat.visible = false;
						}
						else{
					tmpNhat.name = "Nhat";
					if ((currPosition - cs == -2) || (currPosition - cs == 2)) { //Player top
						tmpNhat.x = 200 ;
						tmpNhat.y = 60 ; 
					} else if ((currPosition - cs == -1) || (currPosition - cs == 3)) { //Player right
						tmpNhat.x = 720 ;
						tmpNhat.y = 295 ; 
					} else if ((currPosition - cs == 1) || (currPosition - cs == -3)) { //Player left
						tmpNhat.x = 85 ;
						tmpNhat.y = 295 ; 
					} else {	
						tmpNhat.x = 600 ;
						tmpNhat.y = 500 ; 
					}
						}
					mainStage_mc.addChild(tmpNhat);
				} else if (currTable.ArrPlayers[cs].FinishPosition == 2) {
					tmpResult.Player2_mc.Name_txt.text = currTable.ArrPlayers[cs].PlayerName ;
					tmpResult.Player2_mc.Mark_txt.text = currTable.ArrPlayers[cs].MarkInGame ;
					if (currTable.ArrPlayers[cs].Mark > currTable.MarkUnit) {
						tmpResult.Player2_mc.Money_txt.text = "-" + numberFormat(currTable.MarkUnit, 0, false, true) ;
						currTable.ArrPlayers[cs].Mark = currTable.ArrPlayers[cs].Mark - currTable.MarkUnit ;
					} else { //Diem con khong du tra
						tmpResult.Player2_mc.Money_txt.text = "-" + numberFormat(currTable.ArrPlayers[cs].Mark, 0, false, true) ;
						currTable.ArrPlayers[cs].Mark = 0 ;
					}					
					pos2 = true ;
					//Hien thi bieu tuong
					var tmpNhi : MovieClip = MovieClip(new Nhi()) ;
					tmpNhi.name = "Nhi";
					if ((currPosition - cs == -2) || (currPosition - cs == 2)) { //Player top
						tmpNhi.x = 225 ;
						tmpNhi.y = 70 ; 
					} else if ((currPosition - cs == -1) || (currPosition - cs == 3)) { //Player right
						tmpNhi.x = 750 ;
						tmpNhi.y = 315 ; 
					} else if ((currPosition - cs == 1) || (currPosition - cs == -3)) { //Player left
						tmpNhi.x = 55 ;
						tmpNhi.y = 305 ; 
					} else {	
						tmpNhi.x = 585 ;
						tmpNhi.y = 500 ; 
					}
					mainStage_mc.addChild(tmpNhi);
				} else if (currTable.ArrPlayers[cs].FinishPosition == 3) {
					tmpResult.Player3_mc.Name_txt.text = currTable.ArrPlayers[cs].PlayerName ;
					tmpResult.Player3_mc.Mark_txt.text = currTable.ArrPlayers[cs].MarkInGame ;
					if (currTable.ArrPlayers[cs].Mark > currTable.MarkUnit * 2) {
						tmpResult.Player3_mc.Money_txt.text = "-" + numberFormat(currTable.MarkUnit*2, 0, false, true) ;
						currTable.ArrPlayers[cs].Mark = currTable.ArrPlayers[cs].Mark - currTable.MarkUnit * 2 ;
					} else { //Diem con khong du tra
						tmpResult.Player3_mc.Money_txt.text = "-" + numberFormat(currTable.ArrPlayers[cs].Mark, 0, false, true) ;
						currTable.ArrPlayers[cs].Mark = 0 ;
					}
					pos3 = true ;
					//Hien thi bieu tuong
					var tmpBa : MovieClip = MovieClip(new Ba()) ;
					tmpBa.name = "Ba";
					if ((currPosition - cs == -2) || (currPosition - cs == 2)) { //Player top
						tmpBa.x = 240 ;
						tmpBa.y = 70 ; 
					} else if ((currPosition - cs == -1) || (currPosition - cs == 3)) { //Player right
						tmpBa.x = 750 ;
						tmpBa.y = 315 ; 
					} else if ((currPosition - cs == 1) || (currPosition - cs == -3)) { //Player left
						tmpBa.x = 60 ;
						tmpBa.y = 315 ; 
					} else {	
						tmpBa.x = 580 ;
						tmpBa.y = 515 ; 
					}
					mainStage_mc.addChild(tmpBa);
				} else if (currTable.ArrPlayers[cs].FinishPosition == 4) {					
					tmpResult.Player4_mc.Name_txt.text = currTable.ArrPlayers[cs].PlayerName ;
					tmpResult.Player4_mc.Mark_txt.text = currTable.ArrPlayers[cs].MarkInGame ;
					if (currTable.ArrPlayers[cs].Mark > currTable.MarkUnit*3) {
						tmpResult.Player4_mc.Money_txt.text = "-" + numberFormat(currTable.MarkUnit*3, 0, false, true) ;
						currTable.ArrPlayers[cs].Mark = currTable.ArrPlayers[cs].Mark - currTable.MarkUnit * 3 ;
					} else { //Diem con khong du tra
						tmpResult.Player4_mc.Money_txt.text = "-" + numberFormat(currTable.ArrPlayers[cs].Mark, 0, false, true) ;
						currTable.ArrPlayers[cs].Mark = 0 ;
					}
					pos4 = true ;
					//Hien thi bieu tuong
					var tmpBet : MovieClip = MovieClip(new Bet()) ;
					tmpBet.name = "Bet";
					if ((currPosition - cs == -2) || (currPosition - cs == 2)) { //Player top
						tmpBet.x = 225 ;
						tmpBet.y = 65 ; 
					} else if ((currPosition - cs == -1) || (currPosition - cs == 3)) { //Player right
						tmpBet.x = 750 ;
						tmpBet.y = 315 ; 
					} else if ((currPosition - cs == 1) || (currPosition - cs == -3)) { //Player left
						tmpBet.x = 60 ;
						tmpBet.y = 315 ; 
					} else {	
						tmpBet.x = 585;
						tmpBet.y = 510 ; 
					}
					mainStage_mc.addChild(tmpBet);
				} else if (currTable.ArrPlayers[cs].FinishPosition != 0){
					totalMoney = 0 ;
					if (currTable.ArrPlayers[cs].FinishPosition == 8) { //Duoc den lang do thang khac an lao
						//Tim So tien cua thang bi den
						markPlayer = 0 ;
						for (var o:int = 0; o < object.length; o++) 
						{
							if (object[o].Vitri == 9) {
								for (var p:int = 0; p < currTable.ArrPlayers.length; p++) 
								{
									if (currTable.ArrPlayers[p].PlayerName == object[o].Name) {
										markPlayer = currTable.ArrPlayers[p].Mark ;
										break ;
									}									
								}
								break ;
							}							
						}
						//tf.appendText("Tien:" + markPlayer + "\n") ;
						if (markPlayer > currTable.MarkUnit * 5 * (currTable.CurrentPlayer-1))
							totalMoney = (currTable.MarkUnit * 5 * (currTable.CurrentPlayer-1) * 95) / (100 * (currTable.CurrentPlayer - 1));
						else 
							totalMoney = (markPlayer * 95) / (100 * (currTable.CurrentPlayer - 1));						
					} else if (currTable.ArrPlayers[cs].FinishPosition == 9) { //Bi den lang vi an lao
						if (currTable.ArrPlayers[cs].Mark > currTable.MarkUnit * 5 * (currTable.CurrentPlayer-1))
							totalMoney = 0 - currTable.MarkUnit * 5 * (currTable.CurrentPlayer-1) ;
						else 
							totalMoney = 0 - currTable.ArrPlayers[cs].Mark;
					} else if (currTable.ArrPlayers[cs].FinishPosition == 7) { //Bi den lang vi cho an 3 cay
						if (currTable.ArrPlayers[cs].Mark > currTable.MarkUnit * 5 * (currTable.CurrentPlayer-1))
							totalMoney = 0 - currTable.MarkUnit * 5 * (currTable.CurrentPlayer-1) ;
						else 
							totalMoney = 0 - currTable.ArrPlayers[cs].Mark;
					} else {
						if (currTable.ArrPlayers[cs].Mark > currTable.MarkUnit * (currTable.ArrPlayers[cs].FinishPosition - 1))
							totalMoney = 0 - currTable.MarkUnit * (currTable.ArrPlayers[cs].FinishPosition - 1) ;
						else 
							totalMoney = 0 - currTable.ArrPlayers[cs].Mark ;
					}
					if (!pos1) {
						tmpResult.Player1_mc.Name_txt.text = currTable.ArrPlayers[cs].PlayerName ;
						tmpResult.Player1_mc.Mark_txt.text = currTable.ArrPlayers[cs].MarkInGame ;
						tmpResult.Player1_mc.Money_txt.text = numberFormat(totalMoney, 0, false, true); 
						pos1 = true ;
					} else if (!pos2) {
						tmpResult.Player2_mc.Name_txt.text = currTable.ArrPlayers[cs].PlayerName ;
						tmpResult.Player2_mc.Mark_txt.text = currTable.ArrPlayers[cs].MarkInGame ;
						tmpResult.Player2_mc.Money_txt.text = numberFormat(totalMoney, 0, false, true); 
						pos2 = true ;
					} else if (!pos3) {
						tmpResult.Player3_mc.Name_txt.text = currTable.ArrPlayers[cs].PlayerName ;
						tmpResult.Player3_mc.Mark_txt.text = currTable.ArrPlayers[cs].MarkInGame ;
						tmpResult.Player3_mc.Money_txt.text = numberFormat(totalMoney, 0, false, true); 
						pos3 = true ;
					} else {
						tmpResult.Player4_mc.Name_txt.text = currTable.ArrPlayers[cs].PlayerName ;
						tmpResult.Player4_mc.Mark_txt.text = currTable.ArrPlayers[cs].MarkInGame ;
						tmpResult.Player4_mc.Money_txt.text = numberFormat(totalMoney, 0, false, true); 
						pos4 = true ;
					}
					currTable.ArrPlayers[cs].Mark = currTable.ArrPlayers[cs].Mark + totalMoney ;					
					//Hien thi bieu tuong
					if (currTable.ArrPlayers[cs].FinishPosition == 5) { 
						var tmpMom : MovieClip = MovieClip(new Mom()) ;
						tmpMom.name = "Mom";
						if ((currPosition - cs == -2) || (currPosition - cs == 2)) { //Player top
							tmpMom.x = 155 ;
							tmpMom.y = 40; 
						} else if ((currPosition - cs == -1) || (currPosition - cs == 3)) { //Player right
							tmpMom.x = 655 ;
							tmpMom.y = 265 ; 
						} else if ((currPosition - cs == 1) || (currPosition - cs == -3)) { //Player left
							tmpMom.x = 5 ;
							tmpMom.y = 280 ; 
						} else {	
							tmpMom.x = 510 ;
							tmpMom.y = 475 ; 
						}
							mainStage_mc.addChild(tmpMom);
						} else if (currTable.ArrPlayers[cs].FinishPosition == 6){ //Bet U
						var tmpBet6 : MovieClip = MovieClip(new Bet()) ;
						tmpBet6.name = "Bet";
						if ((currPosition - cs == -2) || (currPosition - cs == 2)) { //Player top
							tmpBet6.x = 225 ;
							tmpBet6.y = 65 ; 
						} else if ((currPosition - cs == -1) || (currPosition - cs == 3)) { //Player right
							tmpBet6.x = 750 ;
							tmpBet6.y = 315 ; 
						} else if ((currPosition - cs == 1) || (currPosition - cs == -3)) { //Player left
							tmpBet6.x = 60 ;
							tmpBet6.y = 315 ; 
						} else {	
							tmpBet6.x = 585;
							tmpBet6.y = 510 ; 
						}
						mainStage_mc.addChild(tmpBet6);
					}
				}
			}
			for (var w:int = 0; w < object.length; w++) 
			{
				var item1 = object[w];
				for (var x:int = 0; x <currTable.ArrPlayers.length; x++) 
				{
					if (currTable.ArrPlayers[x].PlayerName == item1.Name) {
						currTable.ArrPlayers[x].Mark = item1.Money ;
						break ;
					}			
				}
			}
			//Hien thi lai diem cua các Player
			for (var m:int = 0; m < currTable.ArrPlayers.length; m++) 
			{
				if (m == currPosition) {
					mainStage_mc.PlayerBottom_mc.Mark_txt.text = numberFormat(currTable.ArrPlayers[m].Mark, 0, false, true);
				} else if ((currPosition - m == 1) || (currPosition - m == -3)) {
					mainStage_mc.PlayerLeft_mc.Mark_txt.text = numberFormat(currTable.ArrPlayers[m].Mark, 0, false, true);
				} else if ((currPosition - m == -1) || (currPosition - m == 3)) {
					mainStage_mc.PlayerRight_mc.Mark_txt.text = numberFormat(currTable.ArrPlayers[m].Mark, 0, false, true);
				} else if ((currPosition - m == -2) || (currPosition - m == 2)) {
					mainStage_mc.PlayerTop_mc.Mark_txt.text = numberFormat(currTable.ArrPlayers[m].Mark, 0, false, true);
				}
			}
			DisplayColorAgain() ;
			mainStage_mc.btnBackTable.visible = true ;
			if (currPlayer.IsCreateTable) {				
				mainStage_mc.PlayerRight_mc.btnKick_mc.visible = true ;
				if (currTable.CurrentPlayer > 2)
					mainStage_mc.PlayerTop_mc.btnKick_mc.visible = true ;
				if (currTable.CurrentPlayer > 3)
					mainStage_mc.PlayerLeft_mc.btnKick_mc.visible = true ;
			}
			mainStage_mc.addChild(tmpResult);					
		}
		
		public static function btnXacnhan_mc_click(evt : MouseEvent) { // Kick nut Xac nhan tren popup thong bao ket qua
			var tmp :MovieClip = MovieClip(mainStage_mc.getChildByName("Result_Popup")) ;
			mainStage_mc.removeChild(tmp) ;
			//mainStage_mc.bgMain_mc.visible = false ;
			var tmpHide:MovieClip = MovieClip(mainStage_mc.getChildByName("Hide"));
			while (tmpHide != null) {
				mainStage_mc.removeChild(tmpHide) ;
				tmpHide = MovieClip(mainStage_mc.getChildByName("Hide"));
			}
			var tmpU:MovieClip = MovieClip(mainStage_mc.getChildByName("IconU"));
			if (tmpU != null)
				mainStage_mc.removeChild(tmpU) ;
			var tmpDen:MovieClip = MovieClip(mainStage_mc.getChildByName("IconDen"));
			if (tmpDen != null)
				mainStage_mc.removeChild(tmpDen) ;				
			if ((currPlayer.Type != 0) && (mainStage_mc.currentFrame == 5))
				AddBtnReady() ;
				//mainStage_mc.btnReady.visible = true ;			
			currTable.Status = 0 ; //Chuyen trang thai ban ve trang thai cho
			AllowExit = true ;
			if (ArrExitTable != null) 
				if (ArrExitTable.length != 0) {
					for (var j:int = 0; j < ArrExitTable.length; j++) 
					{
						ExitTable(ArrExitTable[j]) ;					
					}
					ArrExitTable = new Array() ;
				}
			if (ArrUpdateMoney != null)
				if (ArrUpdateMoney.length != 0) {
					for (var i:int = 0; i < ArrUpdateMoney.length; i++) 
					{
						UpdateMoney(ArrUpdateMoney[i]) ;					
					}
					ArrUpdateMoney = new Array() ;
				}
			if (strMSG != "") showMSG(strMSG) ;
			var tmpTB : MovieClip = MovieClip(mainStage_mc.getChildByName("ThongBao")) ;
			if (tmpTB != null) {				
				mainStage_mc.removeChild(tmpTB) ;
			}
			if (currPlayer.IsCreateTable)
				mainSocket.sendData("RG:" + currTable.TableID + "ID:" + user.PlayId);
			if (currPlayer.PlayerName == UName)
			{
				//tf.appendText("Call Feed");
				ExternalInterface.call("pushfeed", user.UserId, 4);
			} else if (currPlayer.PlayerName == ChotName) ExternalInterface.call("pushfeed", user.UserId, 5);
			
		}
		
		public static function clickExitTable(evt : MouseEvent):void {	//Kick nut Exit ban		
			//WinMoney = currPlayer.Mark;
			//tf.appendText("\n WIn = " + WinMoney.toString());
			if (currTable.TableID == 0) return ;		
			mainStage_mc.btnBackTable.visible = false ;
			isSend = true ;
			isView = false ;
			var str : String = "LT:" + currTable.TableID + "ID:" + user.PlayId ;			
			mainSocket.sendData(str) ;
			//mainSocket.sendData("hahaha") ;
		}
		
		public static function ExitTable(str : String) { 
			//tf.appendText("\n str exit = " + str);
			if (!AllowExit) {
				ArrExitTable.push(str) ;
				return ;			
			}//Xu ly khi Server tra ve thong tin co nguoi thoat khoi ban
			//{ Tao Tieng + Remove Emotion neu co
			SoundKick()
			var mcEmotion : MovieClip = MovieClip(mainStage_mc.getChildByName("Emotion0")) ;
			if (mcEmotion != null)
				mainStage_mc.removeChild(mcEmotion) ;
			mcEmotion = MovieClip(mainStage_mc.getChildByName("Emotion1")) ;
			if (mcEmotion != null)
				mainStage_mc.removeChild(mcEmotion) ;
			mcEmotion = MovieClip(mainStage_mc.getChildByName("Emotion2")) ;
			if (mcEmotion != null)
				mainStage_mc.removeChild(mcEmotion) ;
			mcEmotion = MovieClip(mainStage_mc.getChildByName("Emotion3")) ;
			if (mcEmotion != null)
				mainStage_mc.removeChild(mcEmotion) ;
			mcEmotion = MovieClip(mainStage_mc.getChildByName("EmotionGroup")) ;
			while (mcEmotion != null) {
				mainStage_mc.removeChild(mcEmotion) ;
				mcEmotion = MovieClip(mainStage_mc.getChildByName("EmotionGroup")) ;
			}	
			//tf.appendText("\n exit talbe 2");	
			try 
			{
				var object = JSON.decode(str) ;
				var cs : int = 0;
				var name : String = "" ;
				if (new int(str) == currTable.TableID) 
					name = user.Username ;
				else
					name = object.Name ;	
				//tf.appendText("Exit Table:" + name + "-" + str) ;				
				if (name == user.Username) { //Neu dung la user thoat
					//Cham dut het thoi gian
					if (mainTimer != null)
						if (mainTimer.running)
							mainTimer.stop() ;					
					if (TakeTimer != null)
						if (TakeTimer.running)
							mainTimer.stop() ;					
					if (tChiaBai != null)
						if (tChiaBai.running)
							tChiaBai.stop() ;
					//tf.appendText("==>Remove bieu tuong") ;
					//{Remove het bieu tuong
					var tmpMark : MovieClip = MovieClip(mainStage_mc.getChildByName("MarkDiscount")) ;
					if (tmpMark != null)
						mainStage_mc.removeChild(tmpMark) ;				
					var tmpAn : MovieClip = MovieClip(mainStage_mc.getChildByName("An")) ;
					if (tmpAn != null)
						mainStage_mc.removeChild(tmpAn) ;				
					var tmpU:MovieClip = MovieClip(mainStage_mc.getChildByName("IconU"));
					if (tmpU != null)
						mainStage_mc.removeChild(tmpU) ;
					var tmpDen:MovieClip = MovieClip(mainStage_mc.getChildByName("IconDen"));
					if (tmpDen != null)
						mainStage_mc.removeChild(tmpDen) ;			
					var tmpNhat:MovieClip = MovieClip(mainStage_mc.getChildByName("Nhat"));
					if (tmpNhat != null)
						mainStage_mc.removeChild(tmpNhat) ;
					var tmpNhi:MovieClip = MovieClip(mainStage_mc.getChildByName("Nhi"));
					if (tmpNhi != null)
						mainStage_mc.removeChild(tmpNhi) ;
					var tmpBa:MovieClip = MovieClip(mainStage_mc.getChildByName("Ba"));
					if (tmpBa != null)
						mainStage_mc.removeChild(tmpBa) ;
					var tmpBet:MovieClip = MovieClip(mainStage_mc.getChildByName("Bet"));
					while (tmpBet != null) {
						mainStage_mc.removeChild(tmpBet) ;
						tmpBet = MovieClip(mainStage_mc.getChildByName("Bet"));
					}
					var tmpMom:MovieClip = MovieClip(mainStage_mc.getChildByName("Mom"));
					while (tmpMom != null) {
						mainStage_mc.removeChild(tmpMom) ;
						tmpMom = MovieClip(mainStage_mc.getChildByName("Mom"));
					}
					mainStage_mc.Card00.visible = false ;	
					mainStage_mc.btnAddCard.visible = false ;
					RemoveBtnRead() ;
					RemoveBtnInitGame() ;
					tf.appendText("\n exit talbe 3");	
					//mainStage_mc.btnReady.visible = false ;
					//}
					//tf.appendText("==>Remove con bai") ;
					//Remove giao dien
					for (var i:int = 0; i < currTable.ArrPlayers.length; i++) 
					{
						if (currTable.ArrPlayers[i].PlayerName == name) {
							currPlayer.Mark = currTable.ArrPlayers[i].Mark ; //Cap nhat lai tien
						}
						if (currTable.ArrPlayers[i].ArrMovie != null) {
							for (var j:int = 0; j < currTable.ArrPlayers[i].ArrMovie.length; j++) 
								mainStage_mc.removeChild(currTable.ArrPlayers[i].ArrMovie[j]) ;					
						}
						if (currTable.ArrPlayers[i].ArrMovieRemove != null) {
							for (var k:int = 0; k < currTable.ArrPlayers[i].ArrMovieRemove.length; k++) 
								mainStage_mc.removeChild(currTable.ArrPlayers[i].ArrMovieRemove[k]) ;					
						}
						if (currTable.ArrPlayers[i].ArrMovieDow != null) {
							for (var l:int = 0; l < currTable.ArrPlayers[i].ArrMovieDow.length; l++) { 
								for (var m:int = 0; m < currTable.ArrPlayers[i].ArrMovieDow[l].length; m++) 
									mainStage_mc.removeChild(currTable.ArrPlayers[i].ArrMovieDow[l][m]) ;							
							}
						}
					}
					for (var i:int = 0; i < currTable.ArrPlayers.length; i++) 
					{
						if (currTable.ArrPlayers[i].PlayerName == currPlayer.PlayerName) {
							currPlayer.Mark = currTable.ArrPlayers[i].Mark ;
							currPlayer.ArrCard = new Array() ;
							currPlayer.ArrCardDow = new Array() ;
							currPlayer.ArrCardRemove = new Array() ;
							currPlayer.ArrMovie = new Array() ;
							currPlayer.ArrMovieDow = new Array() ;
							currPlayer.ArrMovieRemove = new Array() ;
						}					
					}
					tf.appendText("\n ==>Khoi tao frame 4") ;
					//Khoi tao lai toan bo			
					currTable = new ObjectTable() ;
					//{Tro ve khung tao ban
					mainStage_mc.gotoAndStop(4);
					if (mainStage_mc.BG_mc.numChildren > 1)
						mainStage_mc.BG_mc.removeChildAt(1) ;
					if (mainStage_mc.BGListTable_mc.numChildren > 1)
						mainStage_mc.BGListTable_mc.removeChildAt(1) ;
					if (ArrBG[0] != null)
						mainStage_mc.BG_mc.addChild(ArrBG[0]) ;
					if (ArrBG[6] != null) 
						mainStage_mc.BGListTable_mc.addChild(ArrBG[6]) ;
						tf.appendText("\n exit talbe 4");	
					mainStage_mc.btnCreateTable.addEventListener(MouseEvent.CLICK, clickCreateTable) ;			
					mainStage_mc.btnComeInTable.addEventListener(MouseEvent.CLICK, clickComeInTable) ;
					mainStage_mc.btnPlayGame.addEventListener(MouseEvent.CLICK, btnPlayGame) ;
					mainStage_mc.btnBack_mc.addEventListener(MouseEvent.CLICK, btnBack_mc_Click);
					mainStage_mc.btnKhoeBan_mc.addEventListener(MouseEvent.CLICK, btnKhoeBan_mc_Click);
						tf.appendText("\n exit talbe 4 1");	
					if (isBegin) {
							tf.appendText("\n exit talbe 4 ifBegin");	
						mainStage_mc.btnCreateTable.visible = false ;
						mainStage_mc.btnComeInTable.visible = false ;				
						mainStage_mc.btnPlayGame.visible = false ;	
					}
						tf.appendText("\n exit talbe 4 2");	
					//{ Hien thi thong tin nguoi choi
					if ((currPlayer.TinyURL != "") && (currPlayer.TinyURL != null)) {
						var imageLoader:Loader = new Loader();
						var image:URLRequest = new URLRequest(currPlayer.TinyURL) ;
						imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaderBig);				
						imageLoader.name = "BigAW" ;
						imageLoader.load(image);
						function onLoaderBig(evt:Event)
						{
							imageLoader.width = 50 ;
							imageLoader.height = 50 ;
							if (mainStage_mc.Avatar_mc.numChildren > 1) {
								mainStage_mc.Avatar_mc.removeChildAt(1) ;
							}
							mainStage_mc.Avatar_mc.addChild(imageLoader);
						}				
					}	
						tf.appendText("\n exit talbe 4 3");	
					var tmpHide:MovieClip = MovieClip(mainStage_mc.getChildByName("Hide"));			
					while (tmpHide != null) {
						mainStage_mc.removeChild(tmpHide) ;
						tmpHide = MovieClip(mainStage_mc.getChildByName("Hide"));			
					}
					mainStage_mc.Name_txt.text = currPlayer.PlayerName ;
					mainStage_mc.Mark_txt.text = numberFormat(currPlayer.Mark, 0, false, true) + " AG";		
					PlayMoney = currPlayer.Mark;
					var formatText : TextFormat = new TextFormat() ;				
					formatText.font = "Tahoma" ;
					formatText.bold = true ;
					formatText.size = 12 ;
					mainStage_mc.Name_txt.setTextFormat(formatText) ;
					mainStage_mc.Mark_txt.setTextFormat(formatText) ;
					//}
					tf.appendText("\n exit talbe 4 4");	
					//{ Hien thi danh sach phong choi
					for (var o:int = 0; o < movieRooms.length; o++) 
					{
						mainStage_mc.ListRoom_mc.addChild(movieRooms[o]) ;
					}
					//}
					//mainStage_mc.leftFrame2_mc.room_mc.visible = true ; //Hien thi khung room truoc
					//mainStage_mc.leftFrame2_mc.room_mc.addChild(scrRoom) ;
					//}	
					//{ Check Tien hien Popup
					tf.appendText("\n exit talbe 4 5");	
					if ((currPlayer.Mark < 500) && (numberWarning < 2)) {
							//tf.appendText("\n exit talbe 4 5 1");	
						var tmpHide:MovieClip = MovieClip(new Hide());
						tmpHide.name = "Hide";
						tmpHide.x = 0 ;
						tmpHide.y = 0 ;
						tmpHide.alpha = 0 ;
						mainStage_mc.addChild(tmpHide) ;
						numberWarning = numberWarning + 1 ;
						var tmpTB:MovieClip = MovieClip(new Popup_TangAG()) ;
						//tf.appendText("\n exit talbe 4 5 2");	
						if (currPlayer.Mark == 0)
							tmpTB.txtThongbao.text = "Bạn đã hết AG, Bạn có muốn nạp AG ?"	 ;
						else 
							tmpTB.txtThongbao.text = "Bạn đã gần hết AG, Bạn có muốn nạp thêm AG ?"	 ;
						tmpTB.NapAG_btn.addEventListener(MouseEvent.CLICK, NapAG_Popup_Click) ;
						tmpTB.Choitiep_btn.addEventListener(MouseEvent.CLICK, Choitiep_Popup_Click) ;
						tmpTB.name = "TangAG" ;
						tmpTB.x = 240;
						tmpTB.y = 220;
						mainStage_mc.addChild(tmpTB) ;
					}
					tf.appendText("\n exit talbe 4 5 3");	
					//}
					clickRoom(currRoom.RoomID) ;
					tf.appendText("\n exit talbe 4 5 4");	
					if (PlayMoney == InitMoney)
					{
						tf.appendText("ko play");
						ExternalInterface.call("pushfeed", user.UserId);
					}
					else if (PlayMoney - InitMoney > 0)
					{	
						tf.appendText("Play win");
						ExternalInterface.call("pushfeed", user.UserId, 3 );
					}
					else if(PlayMoney - InitMoney < 0)
					{
						tf.appendText("Play lose");
						ExternalInterface.call("pushfeed", user.UserId, 2 );
					}								
					return ;
						//tf.appendText("\n exit talbe 4 5 5");	
				}
				tf.appendText("\n exit talbe 4 6");	
				tf.appendText("Exit Other Table:") ;
				//View lai cac Player
				//{ Remove Quan bai + Emotion cua thang thoat ra
				for (var n:int = 0; n < currTable.ArrPlayers.length; n++) 
				{
					if (currTable.ArrPlayers[n].PlayerName == name) {
						if (currTable.ArrPlayers[n].ArrMovie != null) {
							for (var j1:int = 0; j1 < currTable.ArrPlayers[n].ArrMovie.length; j1++) 
								mainStage_mc.removeChild(currTable.ArrPlayers[n].ArrMovie[j1]) ;					
						}
						if (currTable.ArrPlayers[n].ArrMovieRemove != null) {
							for (var k1:int = 0; k1 < currTable.ArrPlayers[n].ArrMovieRemove.length; k1++) 
								mainStage_mc.removeChild(currTable.ArrPlayers[n].ArrMovieRemove[k1]) ;					
						}
						if (currTable.ArrPlayers[n].ArrMovieDow != null) {
							for (var l1:int = 0; l1 < currTable.ArrPlayers[n].ArrMovieDow.length; l1++) { 
								for (var m1:int = 0; m1 < currTable.ArrPlayers[n].ArrMovieDow[l1].length; m1++) 
									mainStage_mc.removeChild(currTable.ArrPlayers[n].ArrMovieDow[l1][m1]) ;							
							}
						}
						cs = n ;
						currTable.ArrPlayers.splice(n, 1) ;
						break ;
					}
				}
					tf.appendText("\n exit talbe 4 7");	
				//tf.appendText("==>1") ;
				currTable.CurrentPlayer -- ;
				if (cs<currPosition)
					currPosition-- ;
				//{Hien thi len giao dien Player
				mainStage_mc.MarkUnit_txt.text = currTable.MarkUnit + " -- Bàn:" + currTable.TableID ;			
				mainStage_mc.PlayerTop_mc.visible = false ;
				mainStage_mc.PlayerRight_mc.visible = false ;
				mainStage_mc.PlayerLeft_mc.visible = false ;
				//{ Player Hien tai
				LoadData_Bottom(currTable.ArrPlayers[currPosition].PlayerName, currTable.ArrPlayers[currPosition].Mark, currTable.ArrPlayers[currPosition].PlayerIP
								, currTable.ArrPlayers[currPosition].TinyURL, currTable.ArrPlayers[currPosition].Ready) ;
				//}
				var formatText : TextFormat = new TextFormat() ;				
				formatText.font = "Tahoma" ;
				formatText.bold = true ;
				formatText.size = 14 ;
				for (var o:int = 0; o < currTable.ArrPlayers.length; o++) {
					if (o == currPosition) continue ;
					var item: ObjectPlayer	= currTable.ArrPlayers[o];
					if ((currPosition - o == 2) || (currPosition - o == -2)) {
						LoadData_Top(item.PlayerName, item.Mark, item.PlayerIP, item.TinyURL, item.Ready) ;
						if (currPlayer.IsCreateTable)
							mainStage_mc.PlayerTop_mc.btnKick_mc.visible = true ;
					} else if ((currPosition - o == 1) || (currPosition - o == -3)) {
						LoadData_Left(item.PlayerName, item.Mark, item.PlayerIP, item.TinyURL, item.Ready) ;
						if (currPlayer.IsCreateTable)
							mainStage_mc.PlayerLeft_mc.btnKick_mc.visible = true ;
					} else if ((currPosition - o == -1) || (currPosition - o == 3)) {
						LoadData_Right(item.PlayerName, item.Mark, item.PlayerIP, item.TinyURL, item.Ready) ;
						if (currPlayer.IsCreateTable)
							mainStage_mc.PlayerRight_mc.btnKick_mc.visible = true ;
					}
				}	tf.appendText("\n exit talbe 4 8");	
				DisplayColorAgain() ;
				//}
				tf.appendText("\n exit talbe 5");	
				if (currPlayer.IsCreateTable) {
					var countReady : int = 0 ;
					for (var o1:int = 0; o1 < currTable.ArrPlayers.length; o1++) 
					{
						if (currTable.ArrPlayers[o1].Ready == true) countReady++ ;				
					}
					if ((countReady == currTable.ArrPlayers.length) && (countReady > 1)) 
					{	tf.appendText("\n initgame if");	
						AddBtnInitGame() ;
						//mainStage_mc.btnInitGame.visible = true ;
					}
					else 
						RemoveBtnInitGame() ;
						//mainStage_mc.btnInitGame.visible = false ;
				}
				//tf.appendText("end exit");
			} catch (ex : Error) {				
				//tf.appendText("Error:" + ex.getStackTrace());
			}			
		}
		
		public static function moveCard(_mc: MovieClip, xNew:Number, yNew:Number) {
			TweenMax.to(_mc, 0.5, {x:xNew, y:yNew, ease:Expo.easeOut});				
		}			
		
		public static function clickVolumn(myEvent:MouseEvent) { //Kick tat va mo tieng	
			//{ Tao Tieng
			SoundKick()
			//}
			if (isSound) {
				isSound = false ;
				mainStage_mc.btnVolumn.gotoAndStop("inactive") ;
			} else {
				isSound = true ;
				mainStage_mc.btnVolumn.gotoAndStop("active") ;
			}
		}
		
		public static function clickSortCard(myEvent:MouseEvent) { //Kick Xep bai
			//{ Tao Tieng
			SoundKick()
			//}
			if (sortType % 2 == 0)
				SortForSuit() ;		
			else
				SortForNumber() ;
			sortType++ ;				
		}
		/* Sắp xếp danh sách quân bài theo thứ tự number ==> suit */
		public static function SortForSuit() {		
			currTable.ArrPlayers[currPosition].ArrCard.sortOn(["Suit", "NumberCard"], [Array.NUMERIC, Array.NUMERIC]) ;
			//Gan lai toa do cho danh sach con bai ==> Thay doi Movie tuong ung
			for (var k:int = 0; k < currTable.ArrPlayers[currPosition].ArrCard.length; k++) 
			{
				currTable.ArrPlayers[currPosition].ArrCard[k].X = 280 + k * 20 ;
				currTable.ArrPlayers[currPosition].ArrCard[k].Y = 470 ;		
				currTable.ArrPlayers[currPosition].ArrMovie[k].x = 280 + k * 20 ;
				currTable.ArrPlayers[currPosition].ArrMovie[k].y = 470 ;
				var position :Number = currTable.ArrPlayers[currPosition].ArrCard[k].NumberCard + (currTable.ArrPlayers[currPosition].ArrCard[k].Suit - 1) * 13 + 1 ;
				currTable.ArrPlayers[currPosition].ArrMovie[k].gotoAndStop(position) ;	
				if (currTable.ArrPlayers[currPosition].ArrCard[k].State == 2)
					currTable.ArrPlayers[currPosition].ArrMovie[k].vien_mc.visible = true ;
				else
					currTable.ArrPlayers[currPosition].ArrMovie[k].vien_mc.visible = false ;
			}
		}		
		/* Sắp xếp danh sách quân bài theo thứ tự suit ==> number */
		public static function SortForNumber() {
			currTable.ArrPlayers[currPosition].ArrCard.sortOn(["NumberCard", "Suit"], [Array.NUMERIC, Array.NUMERIC]);
			//Gan lai toa do cho danh sach con bai ==> Thay doi Movie tuong ung
			for (var k:int = 0; k < currTable.ArrPlayers[currPosition].ArrCard.length; k++) 
			{
				currTable.ArrPlayers[currPosition].ArrCard[k].X = 280 + k * 20 ;
				currTable.ArrPlayers[currPosition].ArrCard[k].Y = 470 ;	
				currTable.ArrPlayers[currPosition].ArrMovie[k].x = 280 + k * 20 ;
				currTable.ArrPlayers[currPosition].ArrMovie[k].y = 470 ;
				var position :Number = currTable.ArrPlayers[currPosition].ArrCard[k].NumberCard + (currTable.ArrPlayers[currPosition].ArrCard[k].Suit - 1) * 13 + 1 ;
				currTable.ArrPlayers[currPosition].ArrMovie[k].gotoAndStop(position) ;	
				if (currTable.ArrPlayers[currPosition].ArrCard[k].State == 2)
					currTable.ArrPlayers[currPosition].ArrMovie[k].vien_mc.visible = true ;
				else
					currTable.ArrPlayers[currPosition].ArrMovie[k].vien_mc.visible = false ;
			}
			//showCard(currTable.ArrPlayers[currPosition].ArrCard) ;				
		}
		//Hien thi lai cac con bai
		public static function showCard(arrCard : Array) {
			for (var k:int = 0; k < arrCard.length; k++) 
			{
				currTable.ArrPlayers[currPosition].ArrMovie[k].x = arrCard[k].X ;
				currTable.ArrPlayers[currPosition].ArrMovie[k].y = arrCard[k].Y ;
				var position :Number = arrCard[k].NumberCard + (arrCard[k].Suit - 1) * 13 + 1 ;				
				currTable.ArrPlayers[currPosition].ArrMovie[k].gotoAndStop(position) ;
			}
		}
		
		public static function InitChatTime() {
			if (chatTimer.running)
				chatTimer.stop() ;
			chatTimer = new Timer(1000) ;
			chatTimer.repeatCount = 2 ;
			chatTimer.addEventListener(TimerEvent.TIMER, StartChatTimer);				
			chatTimer.start() ;
		}
		public static function StartChatTimer(evt:TimerEvent) : void { //Dem thoi gian cho moi thao tac
			if (chatTimer.currentCount == chatTimer.repeatCount) {
				chatTimer.stop() ;
			}
		}
		
		public static function clickChat(evt : MouseEvent) : void { //Kick Chat
			if (chatTimer.running) return ;
			var txtInput : String = mainStage_mc.txtChat_mc.text ;
			if (txtInput.length > 160)
				txtInput = txtInput.substring(0, 75) ;
			txtInput = trim(txtInput) ;
			if (txtInput == "") return ;
			if (txtInput.length < 1) return ;			
			var object : Object = new Object() ;
			object.ID = currTable.TableID ;
			object.IDName = currPlayer.PlayerID ;
			object.Data = txtInput ;
			mainSocket.sendData("Chat:" + JSON.encode(object)) ;			
			mainStage_mc.txtChat_mc.text = "" ;
			InitChatTime() ;
		}
		public static function Chat(str : String) : void { //Nhan thong tin chat server tra ve
			var object = JSON.decode(str) ;
			if (object.IDName != currPlayer.PlayerName)
				mainStage_mc.txtAreaChat_mc.htmlText += "<font color=\"#0000FF\">" + object.IDName + ":</font>" + object.Data + "\n" ;
			else 
				mainStage_mc.txtAreaChat_mc.htmlText += "<font color=\"#800040\">" + object.IDName + ":</font>" + object.Data + "\n" ;
			//{ thay the Emotion
			//mainStage_mc.txtAreaChat_mc.htmlText = mainStage_mc.txtAreaChat_mc.htmlText + "<img src=\"http://127.0.0.1/Game/flash/1.jpg\"" + " width=\"16\" height=\"16\" hspace=\"0\" vspace=\"0\"/>\n"
			//}
			mainStage_mc.txtAreaChat_mc.verticalScrollPosition = mainStage_mc.txtAreaChat_mc.maxVerticalScrollPosition ;
			if (isSound) {
				try
				{
					if (msgSound == null) {
						msgSound = new Sound(new URLRequest(preURLSound + "Message.mp3"));
						msgSound.addEventListener(Event.COMPLETE, PlaySoundMessage) ;
					} else {
						msgSound.play()
					}
				} catch (e:Error) { }
			}
		}
		public static function PlaySoundMessage(event : Event) {
			msgSound.play() ;
		}
		public static function keyDowChat(evt : KeyboardEvent) { //Xu ly phim trong khung chat
			if (chatTimer.running) return ;
			if (evt.keyCode == 13) {
				var txtInput : String = mainStage_mc.txtChat_mc.text ;
				if (txtInput.length > 160)
					txtInput = txtInput.substring(0, 160) ;
				txtInput = trim(txtInput) ;
				//tf.appendText("*" + txtInput + "*" + txtInput.length + "*") ;
				if (txtInput == "") return ;
				if (txtInput.length < 1) return ;
				var object : Object = new Object() ;
				object.ID = currTable.TableID ;
				object.IDName = currPlayer.PlayerID ;
				object.Data = txtInput ;
				mainSocket.sendData("Chat:" + JSON.encode(object)) ;
				mainStage_mc.txtChat_mc.text = "" ;
				InitChatTime() ;
			}			
		}
		
		public static function KichLeftPlayer(evt : MouseEvent) :void { //Kick Left Player
			//{ Tao Tieng
			SoundKick()
			//}
			var cs : int = currPosition ;
			cs = cs - 1 ;
			if (cs < 0) cs = currTable.CurrentPlayer - 1 ;			
			var object : Object = new Object() ;
			object.ID = currTable.TableID ;
			object.StartID = currPlayer.PlayerID ;
			object.Name = currTable.ArrPlayers[cs].PlayerName ;
			//tf.appendText("Name kick:" + currTable.ArrPlayers[cs].PlayerName) ;
			mainSocket.sendData("KP:" + JSON.encode(object)) ;
		}
		public static function KichRightPlayer(evt : MouseEvent) :void { //Kick Right Player
			//{ Tao Tieng
			SoundKick()
			//}
			var cs : int = currPosition ;
			cs = cs + 1 ;
			if (cs == currTable.CurrentPlayer) cs = 0 ;
			var object : Object = new Object() ;
			object.ID = currTable.TableID ;
			object.StartID = currPlayer.PlayerID ;
			object.Name = currTable.ArrPlayers[cs].PlayerName ;
			mainSocket.sendData("KP:" + JSON.encode(object)) ;
		}
		public static function KichTopPlayer(evt : MouseEvent) :void { //Kick Top Player
			//{ Tao Tieng
			SoundKick()
			//}
			var cs : int = currPosition ;
			cs = cs + 2 ;
			var object : Object = new Object() ;
			object.ID = currTable.TableID ;
			object.StartID = currPlayer.PlayerID ;
			object.Name = currTable.ArrPlayers[cs].PlayerName ;
			mainSocket.sendData("KP:" + JSON.encode(object)) ;
		}
		
		public static function getDataFromJavaScript(str:String):void 
		{
			//tf.appendText("===>GetData:" + str) ;
			if (str.search("PR:") == 0) {
				PromotionAlert(str.substring(3)) ;
			} else if (str.search("TOP:") == 0) {
				TopDG_Trongngay(str.substring(4)) ;				
			} else if (str.search("DGD:") == 0) {
				TopDG_Homtruoc(str.substring(4)) ;				
			} else if (str.search("DGW:") == 0) {
				TopDG_Tuan(str.substring(4)) ;				
			} else if (str.search("DGM:") == 0) {
				TopDG_Thang(str.substring(4)) ;				
			} else if (str.search("CT:") == 0) {
				TopCT_Trongngay(str.substring(3)) ;				
			} else if (str.search("CTD:") == 0) {
				TopCT_Homtruoc(str.substring(4)) ;				
			} else if (str.search("CTW:") == 0) {
				TopCT_Tuan(str.substring(4)) ;				
			} else if (str.search("CTM:") == 0) {
				TopCT_Thang(str.substring(4))}
			//}
			//}
		}
		
		ExternalInterface.addCallback("ServiceStartUp", getDataFromJavaScript);
		
		public static function processDataFromServer(str:String):void { //Xu ly cac chuoi nhan ve tu Server ==> Phan luong
			if (str == "0") return ;
			//tf.appendText("\n userinfo: " + str);
			if (str.search("WC:") == 0) {
				var obj = JSON.decode(str.substring(3));
				user.PlayId = obj.PlayId;//str.substring(3).split('^')[2];
				currPlayer.PlayerID = user.PlayId ;
				currPlayer.PlayerName = obj.PlayName ;
				if (obj.Level == undefined)
				{
					currPlayer.Level = "99" ;
				}else
				{
					currPlayer.Level = obj.Level;
				}
				//currPlayer.Level = ;
				currPlayer.DisplayName = obj.DisName;
				currPlayer.PlayerIP = obj.sIP ;
				currPlayer.Mark = obj.Mark ;
				//currPlayer.BigURL = obj.BigUrl ;
				currPlayer.TinyURL = obj.TinyUrl ; //preURLImage + "Avatar.jpg" ;// obj.TinyUrl ;
				currPlayer.ArrCard = new Array() ;
				currPlayer.ArrMovie = new Array() ;
				currPlayer.ArrCardDow = new Array() ;
				currPlayer.ArrMovieDow = new Array() ;
				currPlayer.ArrCardRemove = new Array() ;
				currPlayer.ArrMovieRemove = new Array() ;
				mainStage_mc.gotoAndStop(2);
				var obj1:Object = new Object();
				obj1.Username = currPlayer.PlayerName;
				ExternalInterface.call("CallPromotion", JSON.encode(obj1));
				//var obj2:Object = new Object();
				//obj2.Username = user.Username;
				//ExternalInterface.call("DayPromotion", JSON.encode(obj2));
				if (currPlayer.Level == 0 || currPlayer.Level ==1 ) {
					TutLoad();
				}
			}
			if (str.search("DC:") == 0) { //Player khac danh bai	
				if (mainStage_mc.currentFrame == 5)
					OtherRemoveCard(str.substring(3)) ;
			} else if (str.search("BC:") == 0) { //Boc bai
				if (mainStage_mc.currentFrame == 5)
					OtherAddCard(str.substring(3)) ;
			} else if (str.search("AC:") == 0) { //An bai
				if (mainStage_mc.currentFrame == 5)
					OtherTakeCard(str.substring(3)) ;
			} else if (str.search("HC:") == 0) { //Ha bai
				if (mainStage_mc.currentFrame == 5)
					OtherDownCard(str.substring(3)) ;
			} else if (str.search("GC:") == 0) { //Gui bai
				if (mainStage_mc.currentFrame == 5)
					OtherAttachCard(str.substring(3)) ;
			} else if (str.search("AL:") == 0) { //Gia tri tra ve sau khi ket noi thanh cong ==> Danh sach Level
				//if (mainStage_mc.currentFrame <= 4)
					initLevelRoom(str.substring(3));				
			} else if (str.search("AR:") == 0) { //Danh sach phong tra ve Add Room sau khi click chon Level
				//if (mainStage_mc.currentFrame <= 5)
					initRoom(str.substring(3));
			} else if (str.search("AT:") == 0) { //Add Table ==> Danh sach ban tra ve				
				if (mainStage_mc.currentFrame == 4)
					initTable(str.substring(3));				
			} 
			else if (str.search("CT:") == 0) { //Create Table
				if (mainStage_mc.currentFrame == 4)
					CreateTable(str.substring(3)) ; 				
			} else if (str.search("RG:") == 0) { //Player khac gui thong bao san sang
				if (mainStage_mc.currentFrame == 5)
					ReadyTable(str.substring(3)) ;
			} else if (str.search("JT:") == 0) { //Join Table
				if ((mainStage_mc.currentFrame == 5) || (mainStage_mc.currentFrame == 4))
					JoinTable(str.substring(3)) ;				
			} else if (str.search("LC:") == 0) { //Khoi tao ban choi
				if (mainStage_mc.currentFrame == 5)
					InitGame(str.substring(3)) ;
			} else if (str.search("VP:") == 0) { //Other Join Table
				if (mainStage_mc.currentFrame == 5)
					OtherJoinTable(str.substring(3)) ;				
			} else if (str.search("LT:") == 0) { //Player roi ban
				if (mainStage_mc.currentFrame == 5)
					ExitTable(str.substring(3)) ;
			} else if (str.search("DT:") == 0) { //Nguoi tao ra ban ban thoat
				if (mainStage_mc.currentFrame == 5)
					ExitTable(str.substring(3)) ;
			} else if (str.search("FG:") == 0) {
				if (mainStage_mc.currentFrame == 5) 
					FinishedGame(str.substring(3)) ;
			} else if (str.search("VT:") == 0) {
				//tf.appendText("==>Get:" + str) ;
				if (mainStage_mc.currentFrame == 4)
					ViewTable(str.substring(3)) ;
			} else if (str.search("DIT:") == 0) {
				if (mainStage_mc.currentFrame == 4)
					DeleteTable(str.substring(4)) ;
			} else if (str.search("AM:") == 0) {
				UpdateMoney(str.substring(3)) ;
			} else if (str.search("MSGT:") == 0) {				
				ResultTransferAG(str.substring(5)) ;
			} else if (str.search("MSG:") == 0) {
				showMSG(str.substring(4)) ;
			} else if (str.search("Chat:") == 0) {
				if (mainStage_mc.currentFrame == 5)
					Chat(str.substring(5)) ;
			} else if (str.search("EA:") == 0) {
				if (mainStage_mc.currentFrame == 5)
					Display_Emotion_Control(str.substring(3)) ;
			} else if (str.search("SKR") == 0) {				
				isKickRoom = true ;
			} else if (str.search("DP:") == 0) {
				QuaTang_fromServer(str.substring(3)) ; }
			  else if (str.search("RAG:") == 0) {
				DisplayHistory(str.substring(4)) ;
			}/* else if (str.search("TOP:") == 0) {
				Top25Athena(str.substring(4)) ;
			} */else if (str.search("ReSend") == 0) {
				//isReSend = true ;
				if (strHCOld != "") {
					mainSocket.sendData(strHCOld) ;		
					strHCOld = "" ;			
					if (strGCOld != "") {
						mainSocket.sendData(strGCOld) ;
						strGCOld = "" ;
					}
				}
				if (strSendOld != "") {
					mainSocket.sendData(strSendOld) ;	
					strSendOld = "" ;
				}
			}
		}
		
		public static function Add_Popup_Thongbao(str : String) : void {
			var tmpHide:MovieClip = MovieClip(new Hide());
			tmpHide.name = "HideCT";
			tmpHide.x = 0 ;
			tmpHide.y = 0 ;
			tmpHide.alpha = 0 ;
			mainStage_mc.addChild(tmpHide) ;
			var tmpTB:MovieClip = MovieClip(new Thongbao());
			//if (ArrPopupBG[4] != null)
			//	tmpTB.BG_mc.addChild(ArrPopupBG[4]) ;
			tmpTB.name = "Thongbao";
			tmpTB.x = 240;
			tmpTB.y = 220;
			tmpTB.txtThongbao.text = str
			tmpTB.btnXacnhan.addEventListener(MouseEvent.CLICK, btnXacnhan_TB_click);						
			mainStage_mc.addChild(tmpTB);
			//ExternalInterface.call("pushfeed",user.UserId);
		}
		public static function NapAG_Popup_Click(evt : MouseEvent) {
			var url:String = "http://tala.athena.vn/ZingCredit.aspx?session_id=" + user.SessionId + "&sign_user=" + user.UserId + "&username=" + user.Username;
            var request:URLRequest = new URLRequest(url);
			navigateToURL(request, "");
		}
		public static function Choitiep_Popup_Click(evt : MouseEvent) {
			var tmp :MovieClip = MovieClip(mainStage_mc.getChildByName("TangAG")) ;
			while (tmp != null) {
				mainStage_mc.removeChild(tmp) ;
				tmp = MovieClip(mainStage_mc.getChildByName("TangAG")) ;
			}	
			var tmpHide:MovieClip = MovieClip(mainStage_mc.getChildByName("Hide"));
			while (tmpHide != null) {
				mainStage_mc.removeChild(tmpHide) ;
				tmpHide = MovieClip(mainStage_mc.getChildByName("Hide"));
			}
		}
		public static function UpdateMoney(str : String) : void { //Hien thi thong bao Tang tien
			var obj = JSON.decode(str) ;
			var mark : Number = 0 ;
			if ((mainStage_mc.currentFrame == 5) && (!AllowExit)) {
				ArrUpdateMoney.push(str) ;
				return ;			
			}
			if(currPlayer.PlayerName == obj.Name) {
				//Hide
				var tmpHide:MovieClip = MovieClip(new Hide());
				tmpHide.name = "Hide";
				tmpHide.x = 0 ;
				tmpHide.y = 0 ;
				tmpHide.alpha = 0 ;
				mainStage_mc.addChild(tmpHide) ;
				var tmpTB:MovieClip = MovieClip(new Popup_TangAG()) ;
				tmpTB.txtThongbao.text = "Bạn vừa được tặng " + obj.Mark + " AG" ;
				tmpTB.NapAG_btn.addEventListener(MouseEvent.CLICK, NapAG_Popup_Click) ;
				tmpTB.Choitiep_btn.addEventListener(MouseEvent.CLICK, Choitiep_Popup_Click) ;
				tmpTB.name = "TangAG" ;
				tmpTB.x = 240;
				tmpTB.y = 320;
				mainStage_mc.addChild(tmpTB) ;
			}
			var formatText : TextFormat = new TextFormat() ;				
			formatText.font = "Tahoma" ;
			formatText.bold = true ;			
			if (mainStage_mc.currentFrame == 4) {				
				currPlayer.Mark = obj.Mark + currPlayer.Mark;
				mainStage_mc.Mark_txt.text = currPlayer.Mark + " AG";
				formatText.size = 12 ;
				mainStage_mc.Mark_txt.setTextFormat(formatText) ;
			} else if (mainStage_mc.currentFrame == 5) {	
				if (!AllowExit) return ;
				var cs : int = 0 ;				
				for (var i:int = 0; i < currTable.ArrPlayers.length; i++) 
				{					
					if (currTable.ArrPlayers[i].PlayerName == obj.Name) {
						currTable.ArrPlayers[i].Mark = currTable.ArrPlayers[i].Mark + obj.Mark ;
						mark = currTable.ArrPlayers[i].Mark ;
						cs = i ;		
						break ;
					}				
				}
				formatText.size = 11 ;
				//Hien thi lai tren giao dien
				if (currPosition - cs == 0) {
					mainStage_mc.PlayerBottom_mc.Mark_txt.text = numberFormat(mark, 0, false, true) ;
				} else if ((currPosition - cs == -1) || (currPosition - cs == 3)) {
					mainStage_mc.PlayerRight_mc.Mark_txt.text = numberFormat(mark, 0, false, true) ;
					mainStage_mc.PlayerRight_mc.Mark_txt.setTextFormat(formatText) ;
				} else if ((currPosition - cs == -2) || (currPosition - cs == 2)) {
					mainStage_mc.PlayerTop_mc.Mark_txt.text = numberFormat(mark, 0, false, true) ;
					mainStage_mc.PlayerTop_mc.Mark_txt.setTextFormat(formatText) ;
				} else if ((currPosition - cs == -3) || (currPosition - cs == 1)) {
					mainStage_mc.PlayerLeft_mc.Mark_txt.text = numberFormat(mark, 0, false, true) ;
					mainStage_mc.PlayerLeft_mc.Mark_txt.setTextFormat(formatText) ;
				}
			} else {
				if (currPlayer.Mark != 2000)
					currPlayer.Mark = obj.Mark + currPlayer.Mark;
				if (mainStage_mc.currentFrame == 3){
					mainStage_mc.Mark_txt.text = numberFormat(currPlayer.Mark, 0, false, true) + " AG";
					formatText.size = 12 ;					
					mainStage_mc.Mark_txt.setTextFormat(formatText) ;
				}
			}
		}
		/********************** Hien thi Thong bao **********************************/
		public static function showMSG(str : String) : void { //Hien thi cac Thong bao Server gui ve
			var tmpHide:MovieClip = MovieClip(mainStage_mc.getChildByName("Hide"));
			while (tmpHide != null) {
				mainStage_mc.removeChild(tmpHide) ;
				tmpHide = MovieClip(mainStage_mc.getChildByName("Hide"));
			}
			var tmpLD:MovieClip = MovieClip(mainStage_mc.getChildByName("Loading"));
			while (tmpLD != null) {
				mainStage_mc.removeChild(tmpLD) ;
				tmpLD = MovieClip(mainStage_mc.getChildByName("Loading"));
			}
			//Hide
			var tmpHide:MovieClip = MovieClip(new Hide());
			tmpHide.name = "Hide";
			tmpHide.x = 0 ;
			tmpHide.y = 0 ;
			tmpHide.alpha = 0 ;
			mainStage_mc.addChild(tmpHide) ;
			Add_Popup_Thongbao(str) ;
		}
		public static function processDisconnectServer() : void {
			isReConnect = true ;
			Add_Popup_Thongbao("Bạn bị đứt kết nối với máy chủ") ;
		}
		public static function PromotionAlert(str : String) {
			var objArr : Array = JSON.decode(str) as Array ;
			for (var i:int = 0; i < objArr.length; i++) 
			{
				if (objArr[i].AG == 0) {
					var tmpTB:MovieClip = MovieClip(new Thongbao1());
					tmpTB.name = "Thongbao";
					tmpTB.x = 240;
					tmpTB.y = 220;
					tmpTB.txtThongbao.text = objArr[i].Message ;
					tmpTB.btnXacnhan.addEventListener(MouseEvent.CLICK, btnXacnhan1_PR_click);
					tmpTB.btnHuybo_1.addEventListener(MouseEvent.CLICK, btnHuybo_1_click);
				} else {
					var tmpTB:MovieClip = MovieClip(new Thongbao());
					tmpTB.name = "Thongbao";
					tmpTB.x = 240;
					tmpTB.y = 220;
					tmpTB.txtThongbao.text = objArr[i].Message ;
					tmpTB.btnXacnhan.addEventListener(MouseEvent.CLICK, btnXacnhan_PR_click);
				}
				//tmpTB.btnHuybo_1.addEventListener(MouseEvent.CLICK, btnHuybo_1_click);
				mainStage_mc.addChild(tmpTB);
			}
		}
		public static function btnHuybo_1_click(evt:MouseEvent) {//Xu ly Kick Huy bo tren popup vao ban
			//{ Tao Tieng
			SoundKick()
			//}
			Clean_Message() ;
		}
		public static function btnXacnhan1_PR_click(evt:MouseEvent) {
			//{ Tao Tieng
			SoundKick()
			//}
			Clean_Message() ;
			var obj1:Object = new Object();
			obj1.Username = currPlayer.PlayerName;
			ExternalInterface.call("UpdatePromotion",JSON.encode(obj1));
			var url:String = "http://game.athena.vn/gamerinfo/gamerinfodetails.aspx?session_id=" + user.SessionId + "&sign_user=" + user.UserId + "&username=" + user.Username;
            var request:URLRequest = new URLRequest(url);
			navigateToURL(request, "");
		}
		public static function btnXacnhan_PR_click(evt:MouseEvent) {
			//{ Tao Tieng
			SoundKick()
			//}
			Clean_Message() ;
			var obj1:Object = new Object();
			obj1.Username = currPlayer.PlayerName;
			ExternalInterface.call("UpdatePromotion",JSON.encode(obj1));			
		}
		
		public static function Clean_Message() : void {
			var tmpHide:MovieClip = MovieClip(mainStage_mc.getChildByName("Hide"));
			while (tmpHide != null) {
				mainStage_mc.removeChild(tmpHide) ;
				tmpHide = MovieClip(mainStage_mc.getChildByName("Hide"));
			}
			tmpHide = MovieClip(mainStage_mc.getChildByName("HideCT"));
			while (tmpHide != null) {
				mainStage_mc.removeChild(tmpHide) ;
				tmpHide = MovieClip(mainStage_mc.getChildByName("HideCT"));
			}
			var tmpLD:MovieClip = MovieClip(mainStage_mc.getChildByName("LoadingCT"));
			while (tmpLD != null) {
				mainStage_mc.removeChild(tmpLD) ;
				tmpLD = MovieClip(mainStage_mc.getChildByName("LoadingCT"));
			}
			tmpLD = MovieClip(mainStage_mc.getChildByName("Loading"));
			while (tmpLD != null) {
				mainStage_mc.removeChild(tmpLD) ;
				tmpLD = MovieClip(mainStage_mc.getChildByName("Loading"));
			}
			var tmp :MovieClip = MovieClip(mainStage_mc.getChildByName("Thongbao")) ;
			while (tmp != null) {
				mainStage_mc.removeChild(tmp) ;
				tmp = MovieClip(mainStage_mc.getChildByName("Thongbao")) ;
			}
		}
		
		public static function btnXacnhan_TB_click(evt:MouseEvent) {
			//{ Tao Tieng
			SoundKick()
			//}
			ExternalInterface.call("pushfeed", user.UserId);
			Clean_Message() ;
			//if (isReConnect) {
				//isReConnect = false ;
				//user.PlayId = "" ;
				//if (TimerConection.running)
					//TimerConection.stop() ;
				//countConnect = 0 ;
				//TimerConection = new Timer(1000) ;
				//TimerConection.repeatCount = 100 ;			
				//TimerConection.addEventListener(TimerEvent.TIMER, StartTimerConection);				
				//TimerConection.start() ;
			//}
		}
	}	
} 
